83f3b11e659e867dc5ce7285c04e1cbabe62666d
[deliverable/binutils-gdb.git] / bfd / elf32-i386.c
1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright (C) 1993-2017 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf-nacl.h"
27 #include "elf-vxworks.h"
28 #include "bfd_stdint.h"
29 #include "objalloc.h"
30 #include "hashtab.h"
31 #include "dwarf2.h"
32 #include "opcode/i386.h"
33
34 /* 386 uses REL relocations instead of RELA. */
35 #define USE_REL 1
36
37 #include "elf/i386.h"
38
39 static reloc_howto_type elf_howto_table[]=
40 {
41 HOWTO(R_386_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
42 bfd_elf_generic_reloc, "R_386_NONE",
43 TRUE, 0x00000000, 0x00000000, FALSE),
44 HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
45 bfd_elf_generic_reloc, "R_386_32",
46 TRUE, 0xffffffff, 0xffffffff, FALSE),
47 HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
48 bfd_elf_generic_reloc, "R_386_PC32",
49 TRUE, 0xffffffff, 0xffffffff, TRUE),
50 HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
51 bfd_elf_generic_reloc, "R_386_GOT32",
52 TRUE, 0xffffffff, 0xffffffff, FALSE),
53 HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
54 bfd_elf_generic_reloc, "R_386_PLT32",
55 TRUE, 0xffffffff, 0xffffffff, TRUE),
56 HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
57 bfd_elf_generic_reloc, "R_386_COPY",
58 TRUE, 0xffffffff, 0xffffffff, FALSE),
59 HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
60 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
61 TRUE, 0xffffffff, 0xffffffff, FALSE),
62 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
64 TRUE, 0xffffffff, 0xffffffff, FALSE),
65 HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
66 bfd_elf_generic_reloc, "R_386_RELATIVE",
67 TRUE, 0xffffffff, 0xffffffff, FALSE),
68 HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
69 bfd_elf_generic_reloc, "R_386_GOTOFF",
70 TRUE, 0xffffffff, 0xffffffff, FALSE),
71 HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
72 bfd_elf_generic_reloc, "R_386_GOTPC",
73 TRUE, 0xffffffff, 0xffffffff, TRUE),
74
75 /* We have a gap in the reloc numbers here.
76 R_386_standard counts the number up to this point, and
77 R_386_ext_offset is the value to subtract from a reloc type of
78 R_386_16 thru R_386_PC8 to form an index into this table. */
79 #define R_386_standard (R_386_GOTPC + 1)
80 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
81
82 /* These relocs are a GNU extension. */
83 HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
84 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
85 TRUE, 0xffffffff, 0xffffffff, FALSE),
86 HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
87 bfd_elf_generic_reloc, "R_386_TLS_IE",
88 TRUE, 0xffffffff, 0xffffffff, FALSE),
89 HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
90 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
91 TRUE, 0xffffffff, 0xffffffff, FALSE),
92 HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
93 bfd_elf_generic_reloc, "R_386_TLS_LE",
94 TRUE, 0xffffffff, 0xffffffff, FALSE),
95 HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
96 bfd_elf_generic_reloc, "R_386_TLS_GD",
97 TRUE, 0xffffffff, 0xffffffff, FALSE),
98 HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
99 bfd_elf_generic_reloc, "R_386_TLS_LDM",
100 TRUE, 0xffffffff, 0xffffffff, FALSE),
101 HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
102 bfd_elf_generic_reloc, "R_386_16",
103 TRUE, 0xffff, 0xffff, FALSE),
104 HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
105 bfd_elf_generic_reloc, "R_386_PC16",
106 TRUE, 0xffff, 0xffff, TRUE),
107 HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
108 bfd_elf_generic_reloc, "R_386_8",
109 TRUE, 0xff, 0xff, FALSE),
110 HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
111 bfd_elf_generic_reloc, "R_386_PC8",
112 TRUE, 0xff, 0xff, TRUE),
113
114 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
115 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
116 /* These are common with Solaris TLS implementation. */
117 HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
118 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
119 TRUE, 0xffffffff, 0xffffffff, FALSE),
120 HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
121 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
122 TRUE, 0xffffffff, 0xffffffff, FALSE),
123 HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
124 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
125 TRUE, 0xffffffff, 0xffffffff, FALSE),
126 HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
127 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
128 TRUE, 0xffffffff, 0xffffffff, FALSE),
129 HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
130 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
131 TRUE, 0xffffffff, 0xffffffff, FALSE),
132 HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
133 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
134 TRUE, 0xffffffff, 0xffffffff, FALSE),
135 HOWTO(R_386_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
136 bfd_elf_generic_reloc, "R_386_SIZE32",
137 TRUE, 0xffffffff, 0xffffffff, FALSE),
138 HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
139 bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
140 TRUE, 0xffffffff, 0xffffffff, FALSE),
141 HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
142 bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
143 FALSE, 0, 0, FALSE),
144 HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
145 bfd_elf_generic_reloc, "R_386_TLS_DESC",
146 TRUE, 0xffffffff, 0xffffffff, FALSE),
147 HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
148 bfd_elf_generic_reloc, "R_386_IRELATIVE",
149 TRUE, 0xffffffff, 0xffffffff, FALSE),
150 HOWTO(R_386_GOT32X, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
151 bfd_elf_generic_reloc, "R_386_GOT32X",
152 TRUE, 0xffffffff, 0xffffffff, FALSE),
153
154 /* Another gap. */
155 #define R_386_ext2 (R_386_GOT32X + 1 - R_386_tls_offset)
156 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_ext2)
157
158 /* GNU extension to record C++ vtable hierarchy. */
159 HOWTO (R_386_GNU_VTINHERIT, /* type */
160 0, /* rightshift */
161 2, /* size (0 = byte, 1 = short, 2 = long) */
162 0, /* bitsize */
163 FALSE, /* pc_relative */
164 0, /* bitpos */
165 complain_overflow_dont, /* complain_on_overflow */
166 NULL, /* special_function */
167 "R_386_GNU_VTINHERIT", /* name */
168 FALSE, /* partial_inplace */
169 0, /* src_mask */
170 0, /* dst_mask */
171 FALSE), /* pcrel_offset */
172
173 /* GNU extension to record C++ vtable member usage. */
174 HOWTO (R_386_GNU_VTENTRY, /* type */
175 0, /* rightshift */
176 2, /* size (0 = byte, 1 = short, 2 = long) */
177 0, /* bitsize */
178 FALSE, /* pc_relative */
179 0, /* bitpos */
180 complain_overflow_dont, /* complain_on_overflow */
181 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
182 "R_386_GNU_VTENTRY", /* name */
183 FALSE, /* partial_inplace */
184 0, /* src_mask */
185 0, /* dst_mask */
186 FALSE) /* pcrel_offset */
187
188 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
189
190 };
191
192 #ifdef DEBUG_GEN_RELOC
193 #define TRACE(str) \
194 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
195 #else
196 #define TRACE(str)
197 #endif
198
199 static reloc_howto_type *
200 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
201 bfd_reloc_code_real_type code)
202 {
203 switch (code)
204 {
205 case BFD_RELOC_NONE:
206 TRACE ("BFD_RELOC_NONE");
207 return &elf_howto_table[R_386_NONE];
208
209 case BFD_RELOC_32:
210 TRACE ("BFD_RELOC_32");
211 return &elf_howto_table[R_386_32];
212
213 case BFD_RELOC_CTOR:
214 TRACE ("BFD_RELOC_CTOR");
215 return &elf_howto_table[R_386_32];
216
217 case BFD_RELOC_32_PCREL:
218 TRACE ("BFD_RELOC_PC32");
219 return &elf_howto_table[R_386_PC32];
220
221 case BFD_RELOC_386_GOT32:
222 TRACE ("BFD_RELOC_386_GOT32");
223 return &elf_howto_table[R_386_GOT32];
224
225 case BFD_RELOC_386_PLT32:
226 TRACE ("BFD_RELOC_386_PLT32");
227 return &elf_howto_table[R_386_PLT32];
228
229 case BFD_RELOC_386_COPY:
230 TRACE ("BFD_RELOC_386_COPY");
231 return &elf_howto_table[R_386_COPY];
232
233 case BFD_RELOC_386_GLOB_DAT:
234 TRACE ("BFD_RELOC_386_GLOB_DAT");
235 return &elf_howto_table[R_386_GLOB_DAT];
236
237 case BFD_RELOC_386_JUMP_SLOT:
238 TRACE ("BFD_RELOC_386_JUMP_SLOT");
239 return &elf_howto_table[R_386_JUMP_SLOT];
240
241 case BFD_RELOC_386_RELATIVE:
242 TRACE ("BFD_RELOC_386_RELATIVE");
243 return &elf_howto_table[R_386_RELATIVE];
244
245 case BFD_RELOC_386_GOTOFF:
246 TRACE ("BFD_RELOC_386_GOTOFF");
247 return &elf_howto_table[R_386_GOTOFF];
248
249 case BFD_RELOC_386_GOTPC:
250 TRACE ("BFD_RELOC_386_GOTPC");
251 return &elf_howto_table[R_386_GOTPC];
252
253 /* These relocs are a GNU extension. */
254 case BFD_RELOC_386_TLS_TPOFF:
255 TRACE ("BFD_RELOC_386_TLS_TPOFF");
256 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
257
258 case BFD_RELOC_386_TLS_IE:
259 TRACE ("BFD_RELOC_386_TLS_IE");
260 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
261
262 case BFD_RELOC_386_TLS_GOTIE:
263 TRACE ("BFD_RELOC_386_TLS_GOTIE");
264 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
265
266 case BFD_RELOC_386_TLS_LE:
267 TRACE ("BFD_RELOC_386_TLS_LE");
268 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
269
270 case BFD_RELOC_386_TLS_GD:
271 TRACE ("BFD_RELOC_386_TLS_GD");
272 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
273
274 case BFD_RELOC_386_TLS_LDM:
275 TRACE ("BFD_RELOC_386_TLS_LDM");
276 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
277
278 case BFD_RELOC_16:
279 TRACE ("BFD_RELOC_16");
280 return &elf_howto_table[R_386_16 - R_386_ext_offset];
281
282 case BFD_RELOC_16_PCREL:
283 TRACE ("BFD_RELOC_16_PCREL");
284 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
285
286 case BFD_RELOC_8:
287 TRACE ("BFD_RELOC_8");
288 return &elf_howto_table[R_386_8 - R_386_ext_offset];
289
290 case BFD_RELOC_8_PCREL:
291 TRACE ("BFD_RELOC_8_PCREL");
292 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
293
294 /* Common with Sun TLS implementation. */
295 case BFD_RELOC_386_TLS_LDO_32:
296 TRACE ("BFD_RELOC_386_TLS_LDO_32");
297 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
298
299 case BFD_RELOC_386_TLS_IE_32:
300 TRACE ("BFD_RELOC_386_TLS_IE_32");
301 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
302
303 case BFD_RELOC_386_TLS_LE_32:
304 TRACE ("BFD_RELOC_386_TLS_LE_32");
305 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
306
307 case BFD_RELOC_386_TLS_DTPMOD32:
308 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
309 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
310
311 case BFD_RELOC_386_TLS_DTPOFF32:
312 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
313 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
314
315 case BFD_RELOC_386_TLS_TPOFF32:
316 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
317 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
318
319 case BFD_RELOC_SIZE32:
320 TRACE ("BFD_RELOC_SIZE32");
321 return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
322
323 case BFD_RELOC_386_TLS_GOTDESC:
324 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
325 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
326
327 case BFD_RELOC_386_TLS_DESC_CALL:
328 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
329 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
330
331 case BFD_RELOC_386_TLS_DESC:
332 TRACE ("BFD_RELOC_386_TLS_DESC");
333 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
334
335 case BFD_RELOC_386_IRELATIVE:
336 TRACE ("BFD_RELOC_386_IRELATIVE");
337 return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
338
339 case BFD_RELOC_386_GOT32X:
340 TRACE ("BFD_RELOC_386_GOT32X");
341 return &elf_howto_table[R_386_GOT32X - R_386_tls_offset];
342
343 case BFD_RELOC_VTABLE_INHERIT:
344 TRACE ("BFD_RELOC_VTABLE_INHERIT");
345 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
346
347 case BFD_RELOC_VTABLE_ENTRY:
348 TRACE ("BFD_RELOC_VTABLE_ENTRY");
349 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
350
351 default:
352 break;
353 }
354
355 TRACE ("Unknown");
356 return 0;
357 }
358
359 static reloc_howto_type *
360 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
361 const char *r_name)
362 {
363 unsigned int i;
364
365 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
366 if (elf_howto_table[i].name != NULL
367 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
368 return &elf_howto_table[i];
369
370 return NULL;
371 }
372
373 static reloc_howto_type *
374 elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
375 {
376 unsigned int indx;
377
378 if ((indx = r_type) >= R_386_standard
379 && ((indx = r_type - R_386_ext_offset) - R_386_standard
380 >= R_386_ext - R_386_standard)
381 && ((indx = r_type - R_386_tls_offset) - R_386_ext
382 >= R_386_ext2 - R_386_ext)
383 && ((indx = r_type - R_386_vt_offset) - R_386_ext2
384 >= R_386_vt - R_386_ext2))
385 {
386 /* xgettext:c-format */
387 _bfd_error_handler (_("%B: invalid relocation type %d"),
388 abfd, (int) r_type);
389 indx = R_386_NONE;
390 }
391 /* PR 17512: file: 0f67f69d. */
392 if (elf_howto_table [indx].type != r_type)
393 return NULL;
394 return &elf_howto_table[indx];
395 }
396
397 static void
398 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
399 arelent *cache_ptr,
400 Elf_Internal_Rela *dst)
401 {
402 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
403 cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
404 }
405
406 /* Return whether a symbol name implies a local label. The UnixWare
407 2.1 cc generates temporary symbols that start with .X, so we
408 recognize them here. FIXME: do other SVR4 compilers also use .X?.
409 If so, we should move the .X recognition into
410 _bfd_elf_is_local_label_name. */
411
412 static bfd_boolean
413 elf_i386_is_local_label_name (bfd *abfd, const char *name)
414 {
415 if (name[0] == '.' && name[1] == 'X')
416 return TRUE;
417
418 return _bfd_elf_is_local_label_name (abfd, name);
419 }
420 \f
421 /* Support for core dump NOTE sections. */
422
423 static bfd_boolean
424 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
425 {
426 int offset;
427 size_t size;
428
429 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
430 {
431 int pr_version = bfd_get_32 (abfd, note->descdata);
432
433 if (pr_version != 1)
434 return FALSE;
435
436 /* pr_cursig */
437 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
438
439 /* pr_pid */
440 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
441
442 /* pr_reg */
443 offset = 28;
444 size = bfd_get_32 (abfd, note->descdata + 8);
445 }
446 else
447 {
448 switch (note->descsz)
449 {
450 default:
451 return FALSE;
452
453 case 144: /* Linux/i386 */
454 /* pr_cursig */
455 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
456
457 /* pr_pid */
458 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
459
460 /* pr_reg */
461 offset = 72;
462 size = 68;
463
464 break;
465 }
466 }
467
468 /* Make a ".reg/999" section. */
469 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
470 size, note->descpos + offset);
471 }
472
473 static bfd_boolean
474 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
475 {
476 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
477 {
478 int pr_version = bfd_get_32 (abfd, note->descdata);
479
480 if (pr_version != 1)
481 return FALSE;
482
483 elf_tdata (abfd)->core->program
484 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
485 elf_tdata (abfd)->core->command
486 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
487 }
488 else
489 {
490 switch (note->descsz)
491 {
492 default:
493 return FALSE;
494
495 case 124: /* Linux/i386 elf_prpsinfo. */
496 elf_tdata (abfd)->core->pid
497 = bfd_get_32 (abfd, note->descdata + 12);
498 elf_tdata (abfd)->core->program
499 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
500 elf_tdata (abfd)->core->command
501 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
502 }
503 }
504
505 /* Note that for some reason, a spurious space is tacked
506 onto the end of the args in some (at least one anyway)
507 implementations, so strip it off if it exists. */
508 {
509 char *command = elf_tdata (abfd)->core->command;
510 int n = strlen (command);
511
512 if (0 < n && command[n - 1] == ' ')
513 command[n - 1] = '\0';
514 }
515
516 return TRUE;
517 }
518 \f
519 /* Functions for the i386 ELF linker.
520
521 In order to gain some understanding of code in this file without
522 knowing all the intricate details of the linker, note the
523 following:
524
525 Functions named elf_i386_* are called by external routines, other
526 functions are only called locally. elf_i386_* functions appear
527 in this file more or less in the order in which they are called
528 from external routines. eg. elf_i386_check_relocs is called
529 early in the link process, elf_i386_finish_dynamic_sections is
530 one of the last functions. */
531
532
533 /* The name of the dynamic interpreter. This is put in the .interp
534 section. */
535
536 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
537
538 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
539 copying dynamic variables from a shared lib into an app's dynbss
540 section, and instead use a dynamic relocation to point into the
541 shared lib. */
542 #define ELIMINATE_COPY_RELOCS 1
543
544 /* The size in bytes of an entry in the procedure linkage table. */
545
546 #define PLT_ENTRY_SIZE 16
547
548 /* The first entry in an absolute procedure linkage table looks like
549 this. See the SVR4 ABI i386 supplement to see how this works.
550 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
551
552 static const bfd_byte elf_i386_plt0_entry[12] =
553 {
554 0xff, 0x35, /* pushl contents of address */
555 0, 0, 0, 0, /* replaced with address of .got + 4. */
556 0xff, 0x25, /* jmp indirect */
557 0, 0, 0, 0 /* replaced with address of .got + 8. */
558 };
559
560 /* Subsequent entries in an absolute procedure linkage table look like
561 this. */
562
563 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
564 {
565 0xff, 0x25, /* jmp indirect */
566 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
567 0x68, /* pushl immediate */
568 0, 0, 0, 0, /* replaced with offset into relocation table. */
569 0xe9, /* jmp relative */
570 0, 0, 0, 0 /* replaced with offset to start of .plt. */
571 };
572
573 /* The first entry in a PIC procedure linkage table look like this.
574 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
575
576 static const bfd_byte elf_i386_pic_plt0_entry[12] =
577 {
578 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
579 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
580 };
581
582 /* Subsequent entries in a PIC procedure linkage table look like this. */
583
584 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
585 {
586 0xff, 0xa3, /* jmp *offset(%ebx) */
587 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
588 0x68, /* pushl immediate */
589 0, 0, 0, 0, /* replaced with offset into relocation table. */
590 0xe9, /* jmp relative */
591 0, 0, 0, 0 /* replaced with offset to start of .plt. */
592 };
593
594 /* Entries in the GOT procedure linkage table look like this. */
595
596 static const bfd_byte elf_i386_got_plt_entry[8] =
597 {
598 0xff, 0x25, /* jmp indirect */
599 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
600 0x66, 0x90 /* xchg %ax,%ax */
601 };
602
603 /* Entries in the PIC GOT procedure linkage table look like this. */
604
605 static const bfd_byte elf_i386_pic_got_plt_entry[8] =
606 {
607 0xff, 0xa3, /* jmp *offset(%ebx) */
608 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
609 0x66, 0x90 /* xchg %ax,%ax */
610 };
611
612 /* .eh_frame covering the .plt section. */
613
614 static const bfd_byte elf_i386_eh_frame_plt[] =
615 {
616 #define PLT_CIE_LENGTH 20
617 #define PLT_FDE_LENGTH 36
618 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
619 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
620 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
621 0, 0, 0, 0, /* CIE ID */
622 1, /* CIE version */
623 'z', 'R', 0, /* Augmentation string */
624 1, /* Code alignment factor */
625 0x7c, /* Data alignment factor */
626 8, /* Return address column */
627 1, /* Augmentation size */
628 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
629 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
630 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
631 DW_CFA_nop, DW_CFA_nop,
632
633 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
634 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
635 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
636 0, 0, 0, 0, /* .plt size goes here */
637 0, /* Augmentation size */
638 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
639 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
640 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
641 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
642 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
643 11, /* Block length */
644 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
645 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
646 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
647 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
648 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
649 };
650
651 /* .eh_frame covering the .plt.got section. */
652
653 static const bfd_byte elf_i386_eh_frame_plt_got[] =
654 {
655 #define PLT_GOT_FDE_LENGTH 16
656 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
657 0, 0, 0, 0, /* CIE ID */
658 1, /* CIE version */
659 'z', 'R', 0, /* Augmentation string */
660 1, /* Code alignment factor */
661 0x7c, /* Data alignment factor */
662 8, /* Return address column */
663 1, /* Augmentation size */
664 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
665 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
666 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
667 DW_CFA_nop, DW_CFA_nop,
668
669 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
670 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
671 0, 0, 0, 0, /* the start of .plt.got goes here */
672 0, 0, 0, 0, /* .plt.got size goes here */
673 0, /* Augmentation size */
674 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
675 };
676
677 struct elf_i386_plt_layout
678 {
679 /* The first entry in an absolute procedure linkage table looks like this. */
680 const bfd_byte *plt0_entry;
681 unsigned int plt0_entry_size;
682
683 /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
684 unsigned int plt0_got1_offset;
685 unsigned int plt0_got2_offset;
686
687 /* Later entries in an absolute procedure linkage table look like this. */
688 const bfd_byte *plt_entry;
689 unsigned int plt_entry_size;
690
691 /* Offsets into plt_entry that are to be replaced with... */
692 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
693 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
694 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
695
696 /* Offset into plt_entry where the initial value of the GOT entry points. */
697 unsigned int plt_lazy_offset;
698
699 /* The first entry in a PIC procedure linkage table looks like this. */
700 const bfd_byte *pic_plt0_entry;
701
702 /* Subsequent entries in a PIC procedure linkage table look like this. */
703 const bfd_byte *pic_plt_entry;
704
705 /* .eh_frame covering the .plt section. */
706 const bfd_byte *eh_frame_plt;
707 unsigned int eh_frame_plt_size;
708
709 /* .eh_frame covering the .plt.got section. */
710 const bfd_byte *eh_frame_plt_got;
711 unsigned int eh_frame_plt_got_size;
712 };
713
714 #define GET_PLT_ENTRY_SIZE(abfd) \
715 get_elf_i386_backend_data (abfd)->plt->plt_entry_size
716
717 /* These are the standard parameters. */
718 static const struct elf_i386_plt_layout elf_i386_plt =
719 {
720 elf_i386_plt0_entry, /* plt0_entry */
721 sizeof (elf_i386_plt0_entry), /* plt0_entry_size */
722 2, /* plt0_got1_offset */
723 8, /* plt0_got2_offset */
724 elf_i386_plt_entry, /* plt_entry */
725 PLT_ENTRY_SIZE, /* plt_entry_size */
726 2, /* plt_got_offset */
727 7, /* plt_reloc_offset */
728 12, /* plt_plt_offset */
729 6, /* plt_lazy_offset */
730 elf_i386_pic_plt0_entry, /* pic_plt0_entry */
731 elf_i386_pic_plt_entry, /* pic_plt_entry */
732 elf_i386_eh_frame_plt, /* eh_frame_plt */
733 sizeof (elf_i386_eh_frame_plt), /* eh_frame_plt_size */
734 elf_i386_eh_frame_plt_got, /* eh_frame_plt_got */
735 sizeof (elf_i386_eh_frame_plt_got), /* eh_frame_plt_got_size */
736 };
737 \f
738
739 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
740 for the PLTResolve stub and then for each PLT entry. */
741 #define PLTRESOLVE_RELOCS_SHLIB 0
742 #define PLTRESOLVE_RELOCS 2
743 #define PLT_NON_JUMP_SLOT_RELOCS 2
744
745 /* Architecture-specific backend data for i386. */
746
747 struct elf_i386_backend_data
748 {
749 /* Parameters describing PLT generation. */
750 const struct elf_i386_plt_layout *plt;
751
752 /* Value used to fill the unused bytes of the first PLT entry. */
753 bfd_byte plt0_pad_byte;
754
755 /* True if the target system is VxWorks. */
756 int is_vxworks;
757 };
758
759 #define get_elf_i386_backend_data(abfd) \
760 ((const struct elf_i386_backend_data *) \
761 get_elf_backend_data (abfd)->arch_data)
762
763 /* These are the standard parameters. */
764 static const struct elf_i386_backend_data elf_i386_arch_bed =
765 {
766 &elf_i386_plt, /* plt */
767 0, /* plt0_pad_byte */
768 0, /* is_vxworks */
769 };
770
771 #define elf_backend_arch_data &elf_i386_arch_bed
772
773 /* Is a undefined weak symbol which is resolved to 0. Reference to an
774 undefined weak symbol is resolved to 0 when building executable if
775 it isn't dynamic and
776 1. Has non-GOT/non-PLT relocations in text section. Or
777 2. Has no GOT/PLT relocation.
778 */
779 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, GOT_RELOC, EH) \
780 ((EH)->elf.root.type == bfd_link_hash_undefweak \
781 && bfd_link_executable (INFO) \
782 && (elf_i386_hash_table (INFO)->interp == NULL \
783 || !(GOT_RELOC) \
784 || (EH)->has_non_got_reloc \
785 || !(INFO)->dynamic_undefined_weak))
786
787 /* i386 ELF linker hash entry. */
788
789 struct elf_i386_link_hash_entry
790 {
791 struct elf_link_hash_entry elf;
792
793 /* Track dynamic relocs copied for this symbol. */
794 struct elf_dyn_relocs *dyn_relocs;
795
796 #define GOT_UNKNOWN 0
797 #define GOT_NORMAL 1
798 #define GOT_TLS_GD 2
799 #define GOT_TLS_IE 4
800 #define GOT_TLS_IE_POS 5
801 #define GOT_TLS_IE_NEG 6
802 #define GOT_TLS_IE_BOTH 7
803 #define GOT_TLS_GDESC 8
804 #define GOT_TLS_GD_BOTH_P(type) \
805 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
806 #define GOT_TLS_GD_P(type) \
807 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
808 #define GOT_TLS_GDESC_P(type) \
809 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
810 #define GOT_TLS_GD_ANY_P(type) \
811 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
812 unsigned char tls_type;
813
814 /* Symbol is referenced by R_386_GOTOFF relocation. */
815 unsigned int gotoff_ref : 1;
816
817 /* Symbol has GOT or PLT relocations. */
818 unsigned int has_got_reloc : 1;
819
820 /* Symbol has non-GOT/non-PLT relocations in text sections. */
821 unsigned int has_non_got_reloc : 1;
822
823 /* Don't call finish_dynamic_symbol on this symbol. */
824 unsigned int no_finish_dynamic_symbol : 1;
825
826 /* 0: symbol isn't ___tls_get_addr.
827 1: symbol is ___tls_get_addr.
828 2: symbol is unknown. */
829 unsigned int tls_get_addr : 2;
830
831 /* Reference count of C/C++ function pointer relocations in read-write
832 section which can be resolved at run-time. */
833 bfd_signed_vma func_pointer_refcount;
834
835 /* Information about the GOT PLT entry. Filled when there are both
836 GOT and PLT relocations against the same function. */
837 union gotplt_union plt_got;
838
839 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
840 starting at the end of the jump table. */
841 bfd_vma tlsdesc_got;
842 };
843
844 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
845
846 struct elf_i386_obj_tdata
847 {
848 struct elf_obj_tdata root;
849
850 /* tls_type for each local got entry. */
851 char *local_got_tls_type;
852
853 /* GOTPLT entries for TLS descriptors. */
854 bfd_vma *local_tlsdesc_gotent;
855 };
856
857 #define elf_i386_tdata(abfd) \
858 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
859
860 #define elf_i386_local_got_tls_type(abfd) \
861 (elf_i386_tdata (abfd)->local_got_tls_type)
862
863 #define elf_i386_local_tlsdesc_gotent(abfd) \
864 (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
865
866 #define is_i386_elf(bfd) \
867 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
868 && elf_tdata (bfd) != NULL \
869 && elf_object_id (bfd) == I386_ELF_DATA)
870
871 static bfd_boolean
872 elf_i386_mkobject (bfd *abfd)
873 {
874 return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
875 I386_ELF_DATA);
876 }
877
878 /* i386 ELF linker hash table. */
879
880 struct elf_i386_link_hash_table
881 {
882 struct elf_link_hash_table elf;
883
884 /* Short-cuts to get to dynamic linker sections. */
885 asection *interp;
886 asection *plt_eh_frame;
887 asection *plt_got;
888 asection *plt_got_eh_frame;
889
890 union
891 {
892 bfd_signed_vma refcount;
893 bfd_vma offset;
894 } tls_ldm_got;
895
896 /* The amount of space used by the reserved portion of the sgotplt
897 section, plus whatever space is used by the jump slots. */
898 bfd_vma sgotplt_jump_table_size;
899
900 /* Small local sym cache. */
901 struct sym_cache sym_cache;
902
903 /* _TLS_MODULE_BASE_ symbol. */
904 struct bfd_link_hash_entry *tls_module_base;
905
906 /* Used by local STT_GNU_IFUNC symbols. */
907 htab_t loc_hash_table;
908 void * loc_hash_memory;
909
910 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */
911 asection *srelplt2;
912
913 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */
914 bfd_vma next_tls_desc_index;
915
916 /* The index of the next unused R_386_JUMP_SLOT slot in .rel.plt. */
917 bfd_vma next_jump_slot_index;
918
919 /* The index of the next unused R_386_IRELATIVE slot in .rel.plt. */
920 bfd_vma next_irelative_index;
921
922 /* TRUE if there are dynamic relocs against IFUNC symbols that apply
923 to read-only sections. */
924 bfd_boolean readonly_dynrelocs_against_ifunc;
925 };
926
927 /* Get the i386 ELF linker hash table from a link_info structure. */
928
929 #define elf_i386_hash_table(p) \
930 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
931 == I386_ELF_DATA ? ((struct elf_i386_link_hash_table *) ((p)->hash)) : NULL)
932
933 #define elf_i386_compute_jump_table_size(htab) \
934 ((htab)->elf.srelplt->reloc_count * 4)
935
936 /* Create an entry in an i386 ELF linker hash table. */
937
938 static struct bfd_hash_entry *
939 elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry,
940 struct bfd_hash_table *table,
941 const char *string)
942 {
943 /* Allocate the structure if it has not already been allocated by a
944 subclass. */
945 if (entry == NULL)
946 {
947 entry = (struct bfd_hash_entry *)
948 bfd_hash_allocate (table, sizeof (struct elf_i386_link_hash_entry));
949 if (entry == NULL)
950 return entry;
951 }
952
953 /* Call the allocation method of the superclass. */
954 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
955 if (entry != NULL)
956 {
957 struct elf_i386_link_hash_entry *eh;
958
959 eh = (struct elf_i386_link_hash_entry *) entry;
960 eh->dyn_relocs = NULL;
961 eh->tls_type = GOT_UNKNOWN;
962 eh->gotoff_ref = 0;
963 eh->has_got_reloc = 0;
964 eh->has_non_got_reloc = 0;
965 eh->no_finish_dynamic_symbol = 0;
966 eh->tls_get_addr = 2;
967 eh->func_pointer_refcount = 0;
968 eh->plt_got.offset = (bfd_vma) -1;
969 eh->tlsdesc_got = (bfd_vma) -1;
970 }
971
972 return entry;
973 }
974
975 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
976 for local symbol so that we can handle local STT_GNU_IFUNC symbols
977 as global symbol. We reuse indx and dynstr_index for local symbol
978 hash since they aren't used by global symbols in this backend. */
979
980 static hashval_t
981 elf_i386_local_htab_hash (const void *ptr)
982 {
983 struct elf_link_hash_entry *h
984 = (struct elf_link_hash_entry *) ptr;
985 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
986 }
987
988 /* Compare local hash entries. */
989
990 static int
991 elf_i386_local_htab_eq (const void *ptr1, const void *ptr2)
992 {
993 struct elf_link_hash_entry *h1
994 = (struct elf_link_hash_entry *) ptr1;
995 struct elf_link_hash_entry *h2
996 = (struct elf_link_hash_entry *) ptr2;
997
998 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
999 }
1000
1001 /* Find and/or create a hash entry for local symbol. */
1002
1003 static struct elf_link_hash_entry *
1004 elf_i386_get_local_sym_hash (struct elf_i386_link_hash_table *htab,
1005 bfd *abfd, const Elf_Internal_Rela *rel,
1006 bfd_boolean create)
1007 {
1008 struct elf_i386_link_hash_entry e, *ret;
1009 asection *sec = abfd->sections;
1010 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
1011 ELF32_R_SYM (rel->r_info));
1012 void **slot;
1013
1014 e.elf.indx = sec->id;
1015 e.elf.dynstr_index = ELF32_R_SYM (rel->r_info);
1016 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
1017 create ? INSERT : NO_INSERT);
1018
1019 if (!slot)
1020 return NULL;
1021
1022 if (*slot)
1023 {
1024 ret = (struct elf_i386_link_hash_entry *) *slot;
1025 return &ret->elf;
1026 }
1027
1028 ret = (struct elf_i386_link_hash_entry *)
1029 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
1030 sizeof (struct elf_i386_link_hash_entry));
1031 if (ret)
1032 {
1033 memset (ret, 0, sizeof (*ret));
1034 ret->elf.indx = sec->id;
1035 ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info);
1036 ret->elf.dynindx = -1;
1037 ret->func_pointer_refcount = 0;
1038 ret->plt_got.offset = (bfd_vma) -1;
1039 *slot = ret;
1040 }
1041 return &ret->elf;
1042 }
1043
1044 /* Destroy an i386 ELF linker hash table. */
1045
1046 static void
1047 elf_i386_link_hash_table_free (bfd *obfd)
1048 {
1049 struct elf_i386_link_hash_table *htab
1050 = (struct elf_i386_link_hash_table *) obfd->link.hash;
1051
1052 if (htab->loc_hash_table)
1053 htab_delete (htab->loc_hash_table);
1054 if (htab->loc_hash_memory)
1055 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
1056 _bfd_elf_link_hash_table_free (obfd);
1057 }
1058
1059 /* Create an i386 ELF linker hash table. */
1060
1061 static struct bfd_link_hash_table *
1062 elf_i386_link_hash_table_create (bfd *abfd)
1063 {
1064 struct elf_i386_link_hash_table *ret;
1065 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
1066
1067 ret = (struct elf_i386_link_hash_table *) bfd_zmalloc (amt);
1068 if (ret == NULL)
1069 return NULL;
1070
1071 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1072 elf_i386_link_hash_newfunc,
1073 sizeof (struct elf_i386_link_hash_entry),
1074 I386_ELF_DATA))
1075 {
1076 free (ret);
1077 return NULL;
1078 }
1079
1080 ret->loc_hash_table = htab_try_create (1024,
1081 elf_i386_local_htab_hash,
1082 elf_i386_local_htab_eq,
1083 NULL);
1084 ret->loc_hash_memory = objalloc_create ();
1085 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1086 {
1087 elf_i386_link_hash_table_free (abfd);
1088 return NULL;
1089 }
1090 ret->elf.root.hash_table_free = elf_i386_link_hash_table_free;
1091
1092 return &ret->elf.root;
1093 }
1094
1095 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
1096 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
1097 hash table. */
1098
1099 static bfd_boolean
1100 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1101 {
1102 struct elf_i386_link_hash_table *htab;
1103
1104 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1105 return FALSE;
1106
1107 htab = elf_i386_hash_table (info);
1108 if (htab == NULL)
1109 return FALSE;
1110
1111 /* Set the contents of the .interp section to the interpreter. */
1112 if (bfd_link_executable (info) && !info->nointerp)
1113 {
1114 asection *s = bfd_get_linker_section (dynobj, ".interp");
1115 if (s == NULL)
1116 abort ();
1117 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1118 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1119 htab->interp = s;
1120 }
1121
1122 if (get_elf_i386_backend_data (dynobj)->is_vxworks
1123 && !elf_vxworks_create_dynamic_sections (dynobj, info,
1124 &htab->srelplt2))
1125 return FALSE;
1126
1127 if (!info->no_ld_generated_unwind_info
1128 && htab->plt_eh_frame == NULL
1129 && htab->elf.splt != NULL)
1130 {
1131 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
1132 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1133 | SEC_LINKER_CREATED);
1134 htab->plt_eh_frame
1135 = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
1136 if (htab->plt_eh_frame == NULL
1137 || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 2))
1138 return FALSE;
1139 }
1140
1141 return TRUE;
1142 }
1143
1144 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1145
1146 static void
1147 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
1148 struct elf_link_hash_entry *dir,
1149 struct elf_link_hash_entry *ind)
1150 {
1151 struct elf_i386_link_hash_entry *edir, *eind;
1152
1153 edir = (struct elf_i386_link_hash_entry *) dir;
1154 eind = (struct elf_i386_link_hash_entry *) ind;
1155
1156 if (eind->dyn_relocs != NULL)
1157 {
1158 if (edir->dyn_relocs != NULL)
1159 {
1160 struct elf_dyn_relocs **pp;
1161 struct elf_dyn_relocs *p;
1162
1163 /* Add reloc counts against the indirect sym to the direct sym
1164 list. Merge any entries against the same section. */
1165 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1166 {
1167 struct elf_dyn_relocs *q;
1168
1169 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1170 if (q->sec == p->sec)
1171 {
1172 q->pc_count += p->pc_count;
1173 q->count += p->count;
1174 *pp = p->next;
1175 break;
1176 }
1177 if (q == NULL)
1178 pp = &p->next;
1179 }
1180 *pp = edir->dyn_relocs;
1181 }
1182
1183 edir->dyn_relocs = eind->dyn_relocs;
1184 eind->dyn_relocs = NULL;
1185 }
1186
1187 if (ind->root.type == bfd_link_hash_indirect
1188 && dir->got.refcount <= 0)
1189 {
1190 edir->tls_type = eind->tls_type;
1191 eind->tls_type = GOT_UNKNOWN;
1192 }
1193
1194 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
1195 generate a R_386_COPY reloc. */
1196 edir->gotoff_ref |= eind->gotoff_ref;
1197
1198 edir->has_got_reloc |= eind->has_got_reloc;
1199 edir->has_non_got_reloc |= eind->has_non_got_reloc;
1200
1201 if (ELIMINATE_COPY_RELOCS
1202 && ind->root.type != bfd_link_hash_indirect
1203 && dir->dynamic_adjusted)
1204 {
1205 /* If called to transfer flags for a weakdef during processing
1206 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1207 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1208 if (dir->versioned != versioned_hidden)
1209 dir->ref_dynamic |= ind->ref_dynamic;
1210 dir->ref_regular |= ind->ref_regular;
1211 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1212 dir->needs_plt |= ind->needs_plt;
1213 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1214 }
1215 else
1216 {
1217 if (eind->func_pointer_refcount > 0)
1218 {
1219 edir->func_pointer_refcount += eind->func_pointer_refcount;
1220 eind->func_pointer_refcount = 0;
1221 }
1222
1223 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1224 }
1225 }
1226
1227 /* Return TRUE if the TLS access code sequence support transition
1228 from R_TYPE. */
1229
1230 static bfd_boolean
1231 elf_i386_check_tls_transition (asection *sec,
1232 bfd_byte *contents,
1233 Elf_Internal_Shdr *symtab_hdr,
1234 struct elf_link_hash_entry **sym_hashes,
1235 unsigned int r_type,
1236 const Elf_Internal_Rela *rel,
1237 const Elf_Internal_Rela *relend)
1238 {
1239 unsigned int val, type, reg;
1240 unsigned long r_symndx;
1241 struct elf_link_hash_entry *h;
1242 bfd_vma offset;
1243 bfd_byte *call;
1244 bfd_boolean indirect_call, tls_get_addr;
1245
1246 offset = rel->r_offset;
1247 switch (r_type)
1248 {
1249 case R_386_TLS_GD:
1250 case R_386_TLS_LDM:
1251 if (offset < 2 || (rel + 1) >= relend)
1252 return FALSE;
1253
1254 indirect_call = FALSE;
1255 call = contents + offset + 4;
1256 val = *(call - 5);
1257 type = *(call - 6);
1258 if (r_type == R_386_TLS_GD)
1259 {
1260 /* Check transition from GD access model. Only
1261 leal foo@tlsgd(,%ebx,1), %eax
1262 call ___tls_get_addr@PLT
1263 or
1264 leal foo@tlsgd(%ebx) %eax
1265 call ___tls_get_addr@PLT
1266 nop
1267 or
1268 leal foo@tlsgd(%reg), %eax
1269 call *___tls_get_addr@GOT(%reg)
1270 which may be converted to
1271 addr32 call ___tls_get_addr
1272 can transit to different access model. */
1273 if ((offset + 10) > sec->size
1274 || (type != 0x8d && type != 0x04))
1275 return FALSE;
1276
1277 if (type == 0x04)
1278 {
1279 /* leal foo@tlsgd(,%ebx,1), %eax
1280 call ___tls_get_addr@PLT */
1281 if (offset < 3)
1282 return FALSE;
1283
1284 if (*(call - 7) != 0x8d
1285 || val != 0x1d
1286 || call[0] != 0xe8)
1287 return FALSE;
1288 }
1289 else
1290 {
1291 /* This must be
1292 leal foo@tlsgd(%ebx), %eax
1293 call ___tls_get_addr@PLT
1294 nop
1295 or
1296 leal foo@tlsgd(%reg), %eax
1297 call *___tls_get_addr@GOT(%reg)
1298 which may be converted to
1299 addr32 call ___tls_get_addr
1300
1301 %eax can't be used as the GOT base register since it
1302 is used to pass parameter to ___tls_get_addr. */
1303 reg = val & 7;
1304 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
1305 return FALSE;
1306
1307 indirect_call = call[0] == 0xff;
1308 if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
1309 && !(call[0] == 0x67 && call[1] == 0xe8)
1310 && !(indirect_call
1311 && (call[1] & 0xf8) == 0x90
1312 && (call[1] & 0x7) == reg))
1313 return FALSE;
1314 }
1315 }
1316 else
1317 {
1318 /* Check transition from LD access model. Only
1319 leal foo@tlsldm(%ebx), %eax
1320 call ___tls_get_addr@PLT
1321 or
1322 leal foo@tlsldm(%reg), %eax
1323 call *___tls_get_addr@GOT(%reg)
1324 which may be converted to
1325 addr32 call ___tls_get_addr
1326 can transit to different access model. */
1327 if (type != 0x8d || (offset + 9) > sec->size)
1328 return FALSE;
1329
1330 /* %eax can't be used as the GOT base register since it is
1331 used to pass parameter to ___tls_get_addr. */
1332 reg = val & 7;
1333 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
1334 return FALSE;
1335
1336 indirect_call = call[0] == 0xff;
1337 if (!(reg == 3 && call[0] == 0xe8)
1338 && !(call[0] == 0x67 && call[1] == 0xe8)
1339 && !(indirect_call
1340 && (call[1] & 0xf8) == 0x90
1341 && (call[1] & 0x7) == reg))
1342 return FALSE;
1343 }
1344
1345 r_symndx = ELF32_R_SYM (rel[1].r_info);
1346 if (r_symndx < symtab_hdr->sh_info)
1347 return FALSE;
1348
1349 tls_get_addr = FALSE;
1350 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1351 if (h != NULL && h->root.root.string != NULL)
1352 {
1353 struct elf_i386_link_hash_entry *eh
1354 = (struct elf_i386_link_hash_entry *) h;
1355 tls_get_addr = eh->tls_get_addr == 1;
1356 if (eh->tls_get_addr > 1)
1357 {
1358 /* Use strncmp to check ___tls_get_addr since
1359 ___tls_get_addr may be versioned. */
1360 if (strncmp (h->root.root.string, "___tls_get_addr", 15)
1361 == 0)
1362 {
1363 eh->tls_get_addr = 1;
1364 tls_get_addr = TRUE;
1365 }
1366 else
1367 eh->tls_get_addr = 0;
1368 }
1369 }
1370
1371 if (!tls_get_addr)
1372 return FALSE;
1373 else if (indirect_call)
1374 return (ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X);
1375 else
1376 return (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1377 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
1378
1379 case R_386_TLS_IE:
1380 /* Check transition from IE access model:
1381 movl foo@indntpoff(%rip), %eax
1382 movl foo@indntpoff(%rip), %reg
1383 addl foo@indntpoff(%rip), %reg
1384 */
1385
1386 if (offset < 1 || (offset + 4) > sec->size)
1387 return FALSE;
1388
1389 /* Check "movl foo@tpoff(%rip), %eax" first. */
1390 val = bfd_get_8 (abfd, contents + offset - 1);
1391 if (val == 0xa1)
1392 return TRUE;
1393
1394 if (offset < 2)
1395 return FALSE;
1396
1397 /* Check movl|addl foo@tpoff(%rip), %reg. */
1398 type = bfd_get_8 (abfd, contents + offset - 2);
1399 return ((type == 0x8b || type == 0x03)
1400 && (val & 0xc7) == 0x05);
1401
1402 case R_386_TLS_GOTIE:
1403 case R_386_TLS_IE_32:
1404 /* Check transition from {IE_32,GOTIE} access model:
1405 subl foo@{tpoff,gontoff}(%reg1), %reg2
1406 movl foo@{tpoff,gontoff}(%reg1), %reg2
1407 addl foo@{tpoff,gontoff}(%reg1), %reg2
1408 */
1409
1410 if (offset < 2 || (offset + 4) > sec->size)
1411 return FALSE;
1412
1413 val = bfd_get_8 (abfd, contents + offset - 1);
1414 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1415 return FALSE;
1416
1417 type = bfd_get_8 (abfd, contents + offset - 2);
1418 return type == 0x8b || type == 0x2b || type == 0x03;
1419
1420 case R_386_TLS_GOTDESC:
1421 /* Check transition from GDesc access model:
1422 leal x@tlsdesc(%ebx), %eax
1423
1424 Make sure it's a leal adding ebx to a 32-bit offset
1425 into any register, although it's probably almost always
1426 going to be eax. */
1427
1428 if (offset < 2 || (offset + 4) > sec->size)
1429 return FALSE;
1430
1431 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1432 return FALSE;
1433
1434 val = bfd_get_8 (abfd, contents + offset - 1);
1435 return (val & 0xc7) == 0x83;
1436
1437 case R_386_TLS_DESC_CALL:
1438 /* Check transition from GDesc access model:
1439 call *x@tlsdesc(%eax)
1440 */
1441 if (offset + 2 <= sec->size)
1442 {
1443 /* Make sure that it's a call *x@tlsdesc(%eax). */
1444 call = contents + offset;
1445 return call[0] == 0xff && call[1] == 0x10;
1446 }
1447
1448 return FALSE;
1449
1450 default:
1451 abort ();
1452 }
1453 }
1454
1455 /* Return TRUE if the TLS access transition is OK or no transition
1456 will be performed. Update R_TYPE if there is a transition. */
1457
1458 static bfd_boolean
1459 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1460 asection *sec, bfd_byte *contents,
1461 Elf_Internal_Shdr *symtab_hdr,
1462 struct elf_link_hash_entry **sym_hashes,
1463 unsigned int *r_type, int tls_type,
1464 const Elf_Internal_Rela *rel,
1465 const Elf_Internal_Rela *relend,
1466 struct elf_link_hash_entry *h,
1467 unsigned long r_symndx,
1468 bfd_boolean from_relocate_section)
1469 {
1470 unsigned int from_type = *r_type;
1471 unsigned int to_type = from_type;
1472 bfd_boolean check = TRUE;
1473
1474 /* Skip TLS transition for functions. */
1475 if (h != NULL
1476 && (h->type == STT_FUNC
1477 || h->type == STT_GNU_IFUNC))
1478 return TRUE;
1479
1480 switch (from_type)
1481 {
1482 case R_386_TLS_GD:
1483 case R_386_TLS_GOTDESC:
1484 case R_386_TLS_DESC_CALL:
1485 case R_386_TLS_IE_32:
1486 case R_386_TLS_IE:
1487 case R_386_TLS_GOTIE:
1488 if (bfd_link_executable (info))
1489 {
1490 if (h == NULL)
1491 to_type = R_386_TLS_LE_32;
1492 else if (from_type != R_386_TLS_IE
1493 && from_type != R_386_TLS_GOTIE)
1494 to_type = R_386_TLS_IE_32;
1495 }
1496
1497 /* When we are called from elf_i386_relocate_section, there may
1498 be additional transitions based on TLS_TYPE. */
1499 if (from_relocate_section)
1500 {
1501 unsigned int new_to_type = to_type;
1502
1503 if (bfd_link_executable (info)
1504 && h != NULL
1505 && h->dynindx == -1
1506 && (tls_type & GOT_TLS_IE))
1507 new_to_type = R_386_TLS_LE_32;
1508
1509 if (to_type == R_386_TLS_GD
1510 || to_type == R_386_TLS_GOTDESC
1511 || to_type == R_386_TLS_DESC_CALL)
1512 {
1513 if (tls_type == GOT_TLS_IE_POS)
1514 new_to_type = R_386_TLS_GOTIE;
1515 else if (tls_type & GOT_TLS_IE)
1516 new_to_type = R_386_TLS_IE_32;
1517 }
1518
1519 /* We checked the transition before when we were called from
1520 elf_i386_check_relocs. We only want to check the new
1521 transition which hasn't been checked before. */
1522 check = new_to_type != to_type && from_type == to_type;
1523 to_type = new_to_type;
1524 }
1525
1526 break;
1527
1528 case R_386_TLS_LDM:
1529 if (bfd_link_executable (info))
1530 to_type = R_386_TLS_LE_32;
1531 break;
1532
1533 default:
1534 return TRUE;
1535 }
1536
1537 /* Return TRUE if there is no transition. */
1538 if (from_type == to_type)
1539 return TRUE;
1540
1541 /* Check if the transition can be performed. */
1542 if (check
1543 && ! elf_i386_check_tls_transition (sec, contents,
1544 symtab_hdr, sym_hashes,
1545 from_type, rel, relend))
1546 {
1547 reloc_howto_type *from, *to;
1548 const char *name;
1549
1550 from = elf_i386_rtype_to_howto (abfd, from_type);
1551 to = elf_i386_rtype_to_howto (abfd, to_type);
1552
1553 if (h)
1554 name = h->root.root.string;
1555 else
1556 {
1557 struct elf_i386_link_hash_table *htab;
1558
1559 htab = elf_i386_hash_table (info);
1560 if (htab == NULL)
1561 name = "*unknown*";
1562 else
1563 {
1564 Elf_Internal_Sym *isym;
1565
1566 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1567 abfd, r_symndx);
1568 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1569 }
1570 }
1571
1572 _bfd_error_handler
1573 /* xgettext:c-format */
1574 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1575 "in section `%A' failed"),
1576 abfd, from->name, to->name, name,
1577 (unsigned long) rel->r_offset, sec);
1578 bfd_set_error (bfd_error_bad_value);
1579 return FALSE;
1580 }
1581
1582 *r_type = to_type;
1583 return TRUE;
1584 }
1585
1586 /* With the local symbol, foo, we convert
1587 mov foo@GOT[(%reg1)], %reg2
1588 to
1589 lea foo[@GOTOFF(%reg1)], %reg2
1590 and convert
1591 call/jmp *foo@GOT[(%reg)]
1592 to
1593 nop call foo/jmp foo nop
1594 When PIC is false, convert
1595 test %reg1, foo@GOT[(%reg2)]
1596 to
1597 test $foo, %reg1
1598 and convert
1599 binop foo@GOT[(%reg1)], %reg2
1600 to
1601 binop $foo, %reg2
1602 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1603 instructions. */
1604
1605 static
1606 bfd_boolean
1607 elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1608 bfd_byte *contents,
1609 Elf_Internal_Rela *irel,
1610 struct elf_link_hash_entry *h,
1611 bfd_boolean *converted,
1612 struct bfd_link_info *link_info)
1613 {
1614 struct elf_i386_link_hash_table *htab;
1615 unsigned int opcode;
1616 unsigned int modrm;
1617 bfd_boolean baseless;
1618 Elf_Internal_Sym *isym;
1619 unsigned int addend;
1620 unsigned int nop;
1621 bfd_vma nop_offset;
1622 bfd_boolean is_pic;
1623 bfd_boolean to_reloc_32;
1624 unsigned int r_type;
1625 unsigned int r_symndx;
1626 bfd_vma roff = irel->r_offset;
1627
1628 if (roff < 2)
1629 return TRUE;
1630
1631 /* Addend for R_386_GOT32X relocations must be 0. */
1632 addend = bfd_get_32 (abfd, contents + roff);
1633 if (addend != 0)
1634 return TRUE;
1635
1636 htab = elf_i386_hash_table (link_info);
1637 is_pic = bfd_link_pic (link_info);
1638
1639 r_type = ELF32_R_TYPE (irel->r_info);
1640 r_symndx = ELF32_R_SYM (irel->r_info);
1641
1642 modrm = bfd_get_8 (abfd, contents + roff - 1);
1643 baseless = (modrm & 0xc7) == 0x5;
1644
1645 if (baseless && is_pic)
1646 {
1647 /* For PIC, disallow R_386_GOT32X without a base register
1648 since we don't know what the GOT base is. */
1649 const char *name;
1650
1651 if (h == NULL)
1652 {
1653 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd,
1654 r_symndx);
1655 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1656 }
1657 else
1658 name = h->root.root.string;
1659
1660 _bfd_error_handler
1661 /* xgettext:c-format */
1662 (_("%B: direct GOT relocation R_386_GOT32X against `%s' without base"
1663 " register can not be used when making a shared object"),
1664 abfd, name);
1665 return FALSE;
1666 }
1667
1668 opcode = bfd_get_8 (abfd, contents + roff - 2);
1669
1670 /* Convert to R_386_32 if PIC is false or there is no base
1671 register. */
1672 to_reloc_32 = !is_pic || baseless;
1673
1674 /* Try to convert R_386_GOT32X. Get the symbol referred to by the
1675 reloc. */
1676 if (h == NULL)
1677 {
1678 if (opcode == 0x0ff)
1679 /* Convert "call/jmp *foo@GOT[(%reg)]". */
1680 goto convert_branch;
1681 else
1682 /* Convert "mov foo@GOT[(%reg1)], %reg2",
1683 "test %reg1, foo@GOT(%reg2)" and
1684 "binop foo@GOT[(%reg1)], %reg2". */
1685 goto convert_load;
1686 }
1687
1688 /* Undefined weak symbol is only bound locally in executable
1689 and its reference is resolved as 0. */
1690 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info, TRUE,
1691 elf_i386_hash_entry (h)))
1692 {
1693 if (opcode == 0xff)
1694 {
1695 /* No direct branch to 0 for PIC. */
1696 if (is_pic)
1697 return TRUE;
1698 else
1699 goto convert_branch;
1700 }
1701 else
1702 {
1703 /* We can convert load of address 0 to R_386_32. */
1704 to_reloc_32 = TRUE;
1705 goto convert_load;
1706 }
1707 }
1708
1709 if (opcode == 0xff)
1710 {
1711 /* We have "call/jmp *foo@GOT[(%reg)]". */
1712 if ((h->root.type == bfd_link_hash_defined
1713 || h->root.type == bfd_link_hash_defweak)
1714 && SYMBOL_REFERENCES_LOCAL (link_info, h))
1715 {
1716 /* The function is locally defined. */
1717 convert_branch:
1718 /* Convert R_386_GOT32X to R_386_PC32. */
1719 if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1720 {
1721 struct elf_i386_link_hash_entry *eh
1722 = (struct elf_i386_link_hash_entry *) h;
1723
1724 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1725 is a nop prefix. */
1726 modrm = 0xe8;
1727 /* To support TLS optimization, always use addr32 prefix
1728 for "call *___tls_get_addr@GOT(%reg)". */
1729 if (eh && eh->tls_get_addr == 1)
1730 {
1731 nop = 0x67;
1732 nop_offset = irel->r_offset - 2;
1733 }
1734 else
1735 {
1736 nop = link_info->call_nop_byte;
1737 if (link_info->call_nop_as_suffix)
1738 {
1739 nop_offset = roff + 3;
1740 irel->r_offset -= 1;
1741 }
1742 else
1743 nop_offset = roff - 2;
1744 }
1745 }
1746 else
1747 {
1748 /* Convert to "jmp foo nop". */
1749 modrm = 0xe9;
1750 nop = NOP_OPCODE;
1751 nop_offset = roff + 3;
1752 irel->r_offset -= 1;
1753 }
1754
1755 bfd_put_8 (abfd, nop, contents + nop_offset);
1756 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1757 /* When converting to PC-relative relocation, we
1758 need to adjust addend by -4. */
1759 bfd_put_32 (abfd, -4, contents + irel->r_offset);
1760 irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1761
1762 *converted = TRUE;
1763 }
1764 }
1765 else
1766 {
1767 /* We have "mov foo@GOT[(%re1g)], %reg2",
1768 "test %reg1, foo@GOT(%reg2)" and
1769 "binop foo@GOT[(%reg1)], %reg2".
1770
1771 Avoid optimizing _DYNAMIC since ld.so may use its
1772 link-time address. */
1773 if (h == htab->elf.hdynamic)
1774 return TRUE;
1775
1776 /* def_regular is set by an assignment in a linker script in
1777 bfd_elf_record_link_assignment. */
1778 if ((h->def_regular
1779 || h->root.type == bfd_link_hash_defined
1780 || h->root.type == bfd_link_hash_defweak)
1781 && SYMBOL_REFERENCES_LOCAL (link_info, h))
1782 {
1783 convert_load:
1784 if (opcode == 0x8b)
1785 {
1786 if (to_reloc_32)
1787 {
1788 /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1789 "mov $foo, %reg2" with R_386_32. */
1790 r_type = R_386_32;
1791 modrm = 0xc0 | (modrm & 0x38) >> 3;
1792 bfd_put_8 (abfd, modrm, contents + roff - 1);
1793 opcode = 0xc7;
1794 }
1795 else
1796 {
1797 /* Convert "mov foo@GOT(%reg1), %reg2" to
1798 "lea foo@GOTOFF(%reg1), %reg2". */
1799 r_type = R_386_GOTOFF;
1800 opcode = 0x8d;
1801 }
1802 }
1803 else
1804 {
1805 /* Only R_386_32 is supported. */
1806 if (!to_reloc_32)
1807 return TRUE;
1808
1809 if (opcode == 0x85)
1810 {
1811 /* Convert "test %reg1, foo@GOT(%reg2)" to
1812 "test $foo, %reg1". */
1813 modrm = 0xc0 | (modrm & 0x38) >> 3;
1814 opcode = 0xf7;
1815 }
1816 else
1817 {
1818 /* Convert "binop foo@GOT(%reg1), %reg2" to
1819 "binop $foo, %reg2". */
1820 modrm = (0xc0
1821 | (modrm & 0x38) >> 3
1822 | (opcode & 0x3c));
1823 opcode = 0x81;
1824 }
1825 bfd_put_8 (abfd, modrm, contents + roff - 1);
1826 r_type = R_386_32;
1827 }
1828
1829 bfd_put_8 (abfd, opcode, contents + roff - 2);
1830 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1831
1832 *converted = TRUE;
1833 }
1834 }
1835
1836 return TRUE;
1837 }
1838
1839 /* Rename some of the generic section flags to better document how they
1840 are used here. */
1841 #define need_convert_load sec_flg0
1842 #define check_relocs_failed sec_flg1
1843
1844 /* Look through the relocs for a section during the first phase, and
1845 calculate needed space in the global offset table, procedure linkage
1846 table, and dynamic reloc sections. */
1847
1848 static bfd_boolean
1849 elf_i386_check_relocs (bfd *abfd,
1850 struct bfd_link_info *info,
1851 asection *sec,
1852 const Elf_Internal_Rela *relocs)
1853 {
1854 struct elf_i386_link_hash_table *htab;
1855 Elf_Internal_Shdr *symtab_hdr;
1856 struct elf_link_hash_entry **sym_hashes;
1857 const Elf_Internal_Rela *rel;
1858 const Elf_Internal_Rela *rel_end;
1859 asection *sreloc;
1860 bfd_byte *contents;
1861 bfd_boolean use_plt_got;
1862
1863 if (bfd_link_relocatable (info))
1864 return TRUE;
1865
1866 /* Don't do anything special with non-loaded, non-alloced sections.
1867 In particular, any relocs in such sections should not affect GOT
1868 and PLT reference counting (ie. we don't allow them to create GOT
1869 or PLT entries), there's no possibility or desire to optimize TLS
1870 relocs, and there's not much point in propagating relocs to shared
1871 libs that the dynamic linker won't relocate. */
1872 if ((sec->flags & SEC_ALLOC) == 0)
1873 return TRUE;
1874
1875 BFD_ASSERT (is_i386_elf (abfd));
1876
1877 htab = elf_i386_hash_table (info);
1878 if (htab == NULL)
1879 {
1880 sec->check_relocs_failed = 1;
1881 return FALSE;
1882 }
1883
1884 /* Get the section contents. */
1885 if (elf_section_data (sec)->this_hdr.contents != NULL)
1886 contents = elf_section_data (sec)->this_hdr.contents;
1887 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1888 {
1889 sec->check_relocs_failed = 1;
1890 return FALSE;
1891 }
1892
1893 use_plt_got = (!get_elf_i386_backend_data (abfd)->is_vxworks
1894 && (get_elf_i386_backend_data (abfd)
1895 == &elf_i386_arch_bed));
1896
1897 symtab_hdr = &elf_symtab_hdr (abfd);
1898 sym_hashes = elf_sym_hashes (abfd);
1899
1900 sreloc = NULL;
1901
1902 rel_end = relocs + sec->reloc_count;
1903 for (rel = relocs; rel < rel_end; rel++)
1904 {
1905 unsigned int r_type;
1906 unsigned long r_symndx;
1907 struct elf_link_hash_entry *h;
1908 struct elf_i386_link_hash_entry *eh;
1909 Elf_Internal_Sym *isym;
1910 const char *name;
1911 bfd_boolean size_reloc;
1912
1913 r_symndx = ELF32_R_SYM (rel->r_info);
1914 r_type = ELF32_R_TYPE (rel->r_info);
1915
1916 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1917 {
1918 /* xgettext:c-format */
1919 _bfd_error_handler (_("%B: bad symbol index: %d"),
1920 abfd, r_symndx);
1921 goto error_return;
1922 }
1923
1924 if (r_symndx < symtab_hdr->sh_info)
1925 {
1926 /* A local symbol. */
1927 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1928 abfd, r_symndx);
1929 if (isym == NULL)
1930 goto error_return;
1931
1932 /* Check relocation against local STT_GNU_IFUNC symbol. */
1933 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1934 {
1935 h = elf_i386_get_local_sym_hash (htab, abfd, rel, TRUE);
1936 if (h == NULL)
1937 goto error_return;
1938
1939 /* Fake a STT_GNU_IFUNC symbol. */
1940 h->type = STT_GNU_IFUNC;
1941 h->def_regular = 1;
1942 h->ref_regular = 1;
1943 h->forced_local = 1;
1944 h->root.type = bfd_link_hash_defined;
1945 }
1946 else
1947 h = NULL;
1948 }
1949 else
1950 {
1951 isym = NULL;
1952 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1953 while (h->root.type == bfd_link_hash_indirect
1954 || h->root.type == bfd_link_hash_warning)
1955 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1956 }
1957
1958 eh = (struct elf_i386_link_hash_entry *) h;
1959 if (h != NULL)
1960 {
1961 switch (r_type)
1962 {
1963 default:
1964 break;
1965
1966 case R_386_GOTOFF:
1967 eh->gotoff_ref = 1;
1968 /* Fall through. */
1969 case R_386_32:
1970 case R_386_PC32:
1971 case R_386_PLT32:
1972 case R_386_GOT32:
1973 case R_386_GOT32X:
1974 if (htab->elf.dynobj == NULL)
1975 htab->elf.dynobj = abfd;
1976 /* Create the ifunc sections for static executables. */
1977 if (h->type == STT_GNU_IFUNC
1978 && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj,
1979 info))
1980 goto error_return;
1981 break;
1982 }
1983
1984 /* It is referenced by a non-shared object. */
1985 h->ref_regular = 1;
1986 h->root.non_ir_ref = 1;
1987
1988 if (h->type == STT_GNU_IFUNC)
1989 elf_tdata (info->output_bfd)->has_gnu_symbols
1990 |= elf_gnu_symbol_ifunc;
1991 }
1992
1993 if (! elf_i386_tls_transition (info, abfd, sec, contents,
1994 symtab_hdr, sym_hashes,
1995 &r_type, GOT_UNKNOWN,
1996 rel, rel_end, h, r_symndx, FALSE))
1997 goto error_return;
1998
1999 switch (r_type)
2000 {
2001 case R_386_TLS_LDM:
2002 htab->tls_ldm_got.refcount += 1;
2003 goto create_got;
2004
2005 case R_386_PLT32:
2006 /* This symbol requires a procedure linkage table entry. We
2007 actually build the entry in adjust_dynamic_symbol,
2008 because this might be a case of linking PIC code which is
2009 never referenced by a dynamic object, in which case we
2010 don't need to generate a procedure linkage table entry
2011 after all. */
2012
2013 /* If this is a local symbol, we resolve it directly without
2014 creating a procedure linkage table entry. */
2015 if (h == NULL)
2016 continue;
2017
2018 eh->has_got_reloc = 1;
2019 h->needs_plt = 1;
2020 h->plt.refcount += 1;
2021 break;
2022
2023 case R_386_SIZE32:
2024 size_reloc = TRUE;
2025 goto do_size;
2026
2027 case R_386_TLS_IE_32:
2028 case R_386_TLS_IE:
2029 case R_386_TLS_GOTIE:
2030 if (!bfd_link_executable (info))
2031 info->flags |= DF_STATIC_TLS;
2032 /* Fall through */
2033
2034 case R_386_GOT32:
2035 case R_386_GOT32X:
2036 case R_386_TLS_GD:
2037 case R_386_TLS_GOTDESC:
2038 case R_386_TLS_DESC_CALL:
2039 /* This symbol requires a global offset table entry. */
2040 {
2041 int tls_type, old_tls_type;
2042
2043 switch (r_type)
2044 {
2045 default:
2046 case R_386_GOT32:
2047 case R_386_GOT32X:
2048 tls_type = GOT_NORMAL;
2049 break;
2050 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
2051 case R_386_TLS_GOTDESC:
2052 case R_386_TLS_DESC_CALL:
2053 tls_type = GOT_TLS_GDESC; break;
2054 case R_386_TLS_IE_32:
2055 if (ELF32_R_TYPE (rel->r_info) == r_type)
2056 tls_type = GOT_TLS_IE_NEG;
2057 else
2058 /* If this is a GD->IE transition, we may use either of
2059 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
2060 tls_type = GOT_TLS_IE;
2061 break;
2062 case R_386_TLS_IE:
2063 case R_386_TLS_GOTIE:
2064 tls_type = GOT_TLS_IE_POS; break;
2065 }
2066
2067 if (h != NULL)
2068 {
2069 h->got.refcount += 1;
2070 old_tls_type = elf_i386_hash_entry(h)->tls_type;
2071 }
2072 else
2073 {
2074 bfd_signed_vma *local_got_refcounts;
2075
2076 /* This is a global offset table entry for a local symbol. */
2077 local_got_refcounts = elf_local_got_refcounts (abfd);
2078 if (local_got_refcounts == NULL)
2079 {
2080 bfd_size_type size;
2081
2082 size = symtab_hdr->sh_info;
2083 size *= (sizeof (bfd_signed_vma)
2084 + sizeof (bfd_vma) + sizeof(char));
2085 local_got_refcounts = (bfd_signed_vma *)
2086 bfd_zalloc (abfd, size);
2087 if (local_got_refcounts == NULL)
2088 goto error_return;
2089 elf_local_got_refcounts (abfd) = local_got_refcounts;
2090 elf_i386_local_tlsdesc_gotent (abfd)
2091 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
2092 elf_i386_local_got_tls_type (abfd)
2093 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
2094 }
2095 local_got_refcounts[r_symndx] += 1;
2096 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
2097 }
2098
2099 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
2100 tls_type |= old_tls_type;
2101 /* If a TLS symbol is accessed using IE at least once,
2102 there is no point to use dynamic model for it. */
2103 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2104 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2105 || (tls_type & GOT_TLS_IE) == 0))
2106 {
2107 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
2108 tls_type = old_tls_type;
2109 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2110 && GOT_TLS_GD_ANY_P (tls_type))
2111 tls_type |= old_tls_type;
2112 else
2113 {
2114 if (h)
2115 name = h->root.root.string;
2116 else
2117 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2118 NULL);
2119 _bfd_error_handler
2120 /* xgettext:c-format */
2121 (_("%B: `%s' accessed both as normal and "
2122 "thread local symbol"),
2123 abfd, name);
2124 bfd_set_error (bfd_error_bad_value);
2125 goto error_return;
2126 }
2127 }
2128
2129 if (old_tls_type != tls_type)
2130 {
2131 if (h != NULL)
2132 elf_i386_hash_entry (h)->tls_type = tls_type;
2133 else
2134 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
2135 }
2136 }
2137 /* Fall through */
2138
2139 case R_386_GOTOFF:
2140 case R_386_GOTPC:
2141 create_got:
2142 if (htab->elf.sgot == NULL)
2143 {
2144 if (htab->elf.dynobj == NULL)
2145 htab->elf.dynobj = abfd;
2146 if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
2147 goto error_return;
2148 }
2149 if (r_type != R_386_TLS_IE)
2150 {
2151 if (eh != NULL)
2152 eh->has_got_reloc = 1;
2153 break;
2154 }
2155 /* Fall through */
2156
2157 case R_386_TLS_LE_32:
2158 case R_386_TLS_LE:
2159 if (eh != NULL)
2160 eh->has_got_reloc = 1;
2161 if (bfd_link_executable (info))
2162 break;
2163 info->flags |= DF_STATIC_TLS;
2164 goto do_relocation;
2165
2166 case R_386_32:
2167 case R_386_PC32:
2168 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2169 eh->has_non_got_reloc = 1;
2170 do_relocation:
2171 /* We are called after all symbols have been resolved. Only
2172 relocation against STT_GNU_IFUNC symbol must go through
2173 PLT. */
2174 if (h != NULL
2175 && (bfd_link_executable (info)
2176 || h->type == STT_GNU_IFUNC))
2177 {
2178 /* If this reloc is in a read-only section, we might
2179 need a copy reloc. We can't check reliably at this
2180 stage whether the section is read-only, as input
2181 sections have not yet been mapped to output sections.
2182 Tentatively set the flag for now, and correct in
2183 adjust_dynamic_symbol. */
2184 h->non_got_ref = 1;
2185
2186 /* We may need a .plt entry if the symbol is a function
2187 defined in a shared lib or is a STT_GNU_IFUNC function
2188 referenced from the code or read-only section. */
2189 if (!h->def_regular
2190 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2191 h->plt.refcount += 1;
2192
2193 if (r_type == R_386_PC32)
2194 {
2195 /* Since something like ".long foo - ." may be used
2196 as pointer, make sure that PLT is used if foo is
2197 a function defined in a shared library. */
2198 if ((sec->flags & SEC_CODE) == 0)
2199 h->pointer_equality_needed = 1;
2200 else if (h->type == STT_GNU_IFUNC
2201 && bfd_link_pic (info))
2202 {
2203 if (isym == NULL)
2204 name = h->root.root.string;
2205 else
2206 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2207 NULL);
2208 _bfd_error_handler
2209 /* xgettext:c-format */
2210 (_("%B: unsupported non-PIC call to IFUNC `%s'"),
2211 abfd, name);
2212 bfd_set_error (bfd_error_bad_value);
2213 goto error_return;
2214 }
2215 }
2216 else
2217 {
2218 h->pointer_equality_needed = 1;
2219 /* R_386_32 can be resolved at run-time. */
2220 if (r_type == R_386_32
2221 && (sec->flags & SEC_READONLY) == 0)
2222 eh->func_pointer_refcount += 1;
2223 }
2224 }
2225
2226 size_reloc = FALSE;
2227 do_size:
2228 /* If we are creating a shared library, and this is a reloc
2229 against a global symbol, or a non PC relative reloc
2230 against a local symbol, then we need to copy the reloc
2231 into the shared library. However, if we are linking with
2232 -Bsymbolic, we do not need to copy a reloc against a
2233 global symbol which is defined in an object we are
2234 including in the link (i.e., DEF_REGULAR is set). At
2235 this point we have not seen all the input files, so it is
2236 possible that DEF_REGULAR is not set now but will be set
2237 later (it is never cleared). In case of a weak definition,
2238 DEF_REGULAR may be cleared later by a strong definition in
2239 a shared library. We account for that possibility below by
2240 storing information in the relocs_copied field of the hash
2241 table entry. A similar situation occurs when creating
2242 shared libraries and symbol visibility changes render the
2243 symbol local.
2244
2245 If on the other hand, we are creating an executable, we
2246 may need to keep relocations for symbols satisfied by a
2247 dynamic library if we manage to avoid copy relocs for the
2248 symbol.
2249
2250 Generate dynamic pointer relocation against STT_GNU_IFUNC
2251 symbol in the non-code section. */
2252 if ((bfd_link_pic (info)
2253 && (r_type != R_386_PC32
2254 || (h != NULL
2255 && (! (bfd_link_pie (info)
2256 || SYMBOLIC_BIND (info, h))
2257 || h->root.type == bfd_link_hash_defweak
2258 || !h->def_regular))))
2259 || (h != NULL
2260 && h->type == STT_GNU_IFUNC
2261 && r_type == R_386_32
2262 && (sec->flags & SEC_CODE) == 0)
2263 || (ELIMINATE_COPY_RELOCS
2264 && !bfd_link_pic (info)
2265 && h != NULL
2266 && (h->root.type == bfd_link_hash_defweak
2267 || !h->def_regular)))
2268 {
2269 struct elf_dyn_relocs *p;
2270 struct elf_dyn_relocs **head;
2271
2272 /* We must copy these reloc types into the output file.
2273 Create a reloc section in dynobj and make room for
2274 this reloc. */
2275 if (sreloc == NULL)
2276 {
2277 if (htab->elf.dynobj == NULL)
2278 htab->elf.dynobj = abfd;
2279
2280 sreloc = _bfd_elf_make_dynamic_reloc_section
2281 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
2282
2283 if (sreloc == NULL)
2284 goto error_return;
2285 }
2286
2287 /* If this is a global symbol, we count the number of
2288 relocations we need for this symbol. */
2289 if (h != NULL)
2290 {
2291 head = &eh->dyn_relocs;
2292 }
2293 else
2294 {
2295 /* Track dynamic relocs needed for local syms too.
2296 We really need local syms available to do this
2297 easily. Oh well. */
2298 void **vpp;
2299 asection *s;
2300
2301 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2302 abfd, r_symndx);
2303 if (isym == NULL)
2304 goto error_return;
2305
2306 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2307 if (s == NULL)
2308 s = sec;
2309
2310 vpp = &elf_section_data (s)->local_dynrel;
2311 head = (struct elf_dyn_relocs **)vpp;
2312 }
2313
2314 p = *head;
2315 if (p == NULL || p->sec != sec)
2316 {
2317 bfd_size_type amt = sizeof *p;
2318 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
2319 amt);
2320 if (p == NULL)
2321 goto error_return;
2322 p->next = *head;
2323 *head = p;
2324 p->sec = sec;
2325 p->count = 0;
2326 p->pc_count = 0;
2327 }
2328
2329 p->count += 1;
2330 /* Count size relocation as PC-relative relocation. */
2331 if (r_type == R_386_PC32 || size_reloc)
2332 p->pc_count += 1;
2333 }
2334 break;
2335
2336 /* This relocation describes the C++ object vtable hierarchy.
2337 Reconstruct it for later use during GC. */
2338 case R_386_GNU_VTINHERIT:
2339 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2340 goto error_return;
2341 break;
2342
2343 /* This relocation describes which C++ vtable entries are actually
2344 used. Record for later use during GC. */
2345 case R_386_GNU_VTENTRY:
2346 BFD_ASSERT (h != NULL);
2347 if (h != NULL
2348 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
2349 goto error_return;
2350 break;
2351
2352 default:
2353 break;
2354 }
2355
2356 if (use_plt_got
2357 && h != NULL
2358 && h->plt.refcount > 0
2359 && (((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
2360 || h->got.refcount > 0)
2361 && htab->plt_got == NULL)
2362 {
2363 /* Create the GOT procedure linkage table. */
2364 unsigned int plt_got_align;
2365 const struct elf_backend_data *bed;
2366
2367 bed = get_elf_backend_data (info->output_bfd);
2368 BFD_ASSERT (sizeof (elf_i386_got_plt_entry) == 8
2369 && (sizeof (elf_i386_got_plt_entry)
2370 == sizeof (elf_i386_pic_got_plt_entry)));
2371 plt_got_align = 3;
2372
2373 if (htab->elf.dynobj == NULL)
2374 htab->elf.dynobj = abfd;
2375 htab->plt_got
2376 = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2377 ".plt.got",
2378 (bed->dynamic_sec_flags
2379 | SEC_ALLOC
2380 | SEC_CODE
2381 | SEC_LOAD
2382 | SEC_READONLY));
2383 if (htab->plt_got == NULL
2384 || !bfd_set_section_alignment (htab->elf.dynobj,
2385 htab->plt_got,
2386 plt_got_align))
2387 goto error_return;
2388
2389 if (!info->no_ld_generated_unwind_info
2390 && htab->plt_got_eh_frame == NULL
2391 && get_elf_i386_backend_data (abfd)->plt->eh_frame_plt_got != NULL)
2392 {
2393 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2394 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2395 | SEC_LINKER_CREATED);
2396 htab->plt_got_eh_frame
2397 = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2398 ".eh_frame",
2399 flags);
2400 if (htab->plt_got_eh_frame == NULL
2401 || !bfd_set_section_alignment (htab->elf.dynobj,
2402 htab->plt_got_eh_frame,
2403 2))
2404 goto error_return;
2405 }
2406 }
2407
2408 if (r_type == R_386_GOT32X
2409 && (h == NULL || h->type != STT_GNU_IFUNC))
2410 sec->need_convert_load = 1;
2411 }
2412
2413 if (elf_section_data (sec)->this_hdr.contents != contents)
2414 {
2415 if (!info->keep_memory)
2416 free (contents);
2417 else
2418 {
2419 /* Cache the section contents for elf_link_input_bfd. */
2420 elf_section_data (sec)->this_hdr.contents = contents;
2421 }
2422 }
2423
2424 return TRUE;
2425
2426 error_return:
2427 if (elf_section_data (sec)->this_hdr.contents != contents)
2428 free (contents);
2429 sec->check_relocs_failed = 1;
2430 return FALSE;
2431 }
2432
2433 /* Return the section that should be marked against GC for a given
2434 relocation. */
2435
2436 static asection *
2437 elf_i386_gc_mark_hook (asection *sec,
2438 struct bfd_link_info *info,
2439 Elf_Internal_Rela *rel,
2440 struct elf_link_hash_entry *h,
2441 Elf_Internal_Sym *sym)
2442 {
2443 if (h != NULL)
2444 switch (ELF32_R_TYPE (rel->r_info))
2445 {
2446 case R_386_GNU_VTINHERIT:
2447 case R_386_GNU_VTENTRY:
2448 return NULL;
2449 }
2450
2451 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2452 }
2453
2454 /* Remove undefined weak symbol from the dynamic symbol table if it
2455 is resolved to 0. */
2456
2457 static bfd_boolean
2458 elf_i386_fixup_symbol (struct bfd_link_info *info,
2459 struct elf_link_hash_entry *h)
2460 {
2461 if (h->dynindx != -1
2462 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2463 elf_i386_hash_entry (h)->has_got_reloc,
2464 elf_i386_hash_entry (h)))
2465 {
2466 h->dynindx = -1;
2467 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2468 h->dynstr_index);
2469 }
2470 return TRUE;
2471 }
2472
2473 /* Adjust a symbol defined by a dynamic object and referenced by a
2474 regular object. The current definition is in some section of the
2475 dynamic object, but we're not including those sections. We have to
2476 change the definition to something the rest of the link can
2477 understand. */
2478
2479 static bfd_boolean
2480 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
2481 struct elf_link_hash_entry *h)
2482 {
2483 struct elf_i386_link_hash_table *htab;
2484 asection *s, *srel;
2485 struct elf_i386_link_hash_entry *eh;
2486 struct elf_dyn_relocs *p;
2487
2488 /* STT_GNU_IFUNC symbol must go through PLT. */
2489 if (h->type == STT_GNU_IFUNC)
2490 {
2491 /* All local STT_GNU_IFUNC references must be treate as local
2492 calls via local PLT. */
2493 if (h->ref_regular
2494 && SYMBOL_CALLS_LOCAL (info, h))
2495 {
2496 bfd_size_type pc_count = 0, count = 0;
2497 struct elf_dyn_relocs **pp;
2498
2499 eh = (struct elf_i386_link_hash_entry *) h;
2500 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2501 {
2502 pc_count += p->pc_count;
2503 p->count -= p->pc_count;
2504 p->pc_count = 0;
2505 count += p->count;
2506 if (p->count == 0)
2507 *pp = p->next;
2508 else
2509 pp = &p->next;
2510 }
2511
2512 if (pc_count || count)
2513 {
2514 h->non_got_ref = 1;
2515 if (pc_count)
2516 {
2517 /* Increment PLT reference count only for PC-relative
2518 references. */
2519 h->needs_plt = 1;
2520 if (h->plt.refcount <= 0)
2521 h->plt.refcount = 1;
2522 else
2523 h->plt.refcount += 1;
2524 }
2525 }
2526 }
2527
2528 if (h->plt.refcount <= 0)
2529 {
2530 h->plt.offset = (bfd_vma) -1;
2531 h->needs_plt = 0;
2532 }
2533 return TRUE;
2534 }
2535
2536 /* If this is a function, put it in the procedure linkage table. We
2537 will fill in the contents of the procedure linkage table later,
2538 when we know the address of the .got section. */
2539 if (h->type == STT_FUNC
2540 || h->needs_plt)
2541 {
2542 if (h->plt.refcount <= 0
2543 || SYMBOL_CALLS_LOCAL (info, h)
2544 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2545 && h->root.type == bfd_link_hash_undefweak))
2546 {
2547 /* This case can occur if we saw a PLT32 reloc in an input
2548 file, but the symbol was never referred to by a dynamic
2549 object, or if all references were garbage collected. In
2550 such a case, we don't actually need to build a procedure
2551 linkage table, and we can just do a PC32 reloc instead. */
2552 h->plt.offset = (bfd_vma) -1;
2553 h->needs_plt = 0;
2554 }
2555
2556 return TRUE;
2557 }
2558 else
2559 /* It's possible that we incorrectly decided a .plt reloc was
2560 needed for an R_386_PC32 reloc to a non-function sym in
2561 check_relocs. We can't decide accurately between function and
2562 non-function syms in check-relocs; Objects loaded later in
2563 the link may change h->type. So fix it now. */
2564 h->plt.offset = (bfd_vma) -1;
2565
2566 /* If this is a weak symbol, and there is a real definition, the
2567 processor independent code will have arranged for us to see the
2568 real definition first, and we can just use the same value. */
2569 if (h->u.weakdef != NULL)
2570 {
2571 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2572 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2573 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2574 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2575 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
2576 h->non_got_ref = h->u.weakdef->non_got_ref;
2577 return TRUE;
2578 }
2579
2580 /* This is a reference to a symbol defined by a dynamic object which
2581 is not a function. */
2582
2583 /* If we are creating a shared library, we must presume that the
2584 only references to the symbol are via the global offset table.
2585 For such cases we need not do anything here; the relocations will
2586 be handled correctly by relocate_section. */
2587 if (!bfd_link_executable (info))
2588 return TRUE;
2589
2590 /* If there are no references to this symbol that do not use the
2591 GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
2592 reloc. */
2593 eh = (struct elf_i386_link_hash_entry *) h;
2594 if (!h->non_got_ref && !eh->gotoff_ref)
2595 return TRUE;
2596
2597 /* If -z nocopyreloc was given, we won't generate them either. */
2598 if (info->nocopyreloc)
2599 {
2600 h->non_got_ref = 0;
2601 return TRUE;
2602 }
2603
2604 htab = elf_i386_hash_table (info);
2605 if (htab == NULL)
2606 return FALSE;
2607
2608 /* If there aren't any dynamic relocs in read-only sections nor
2609 R_386_GOTOFF relocation, then we can keep the dynamic relocs and
2610 avoid the copy reloc. This doesn't work on VxWorks, where we can
2611 not have dynamic relocations (other than copy and jump slot
2612 relocations) in an executable. */
2613 if (ELIMINATE_COPY_RELOCS
2614 && !eh->gotoff_ref
2615 && !get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
2616 {
2617 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2618 {
2619 s = p->sec->output_section;
2620 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2621 break;
2622 }
2623
2624 if (p == NULL)
2625 {
2626 h->non_got_ref = 0;
2627 return TRUE;
2628 }
2629 }
2630
2631 /* We must allocate the symbol in our .dynbss section, which will
2632 become part of the .bss section of the executable. There will be
2633 an entry for this symbol in the .dynsym section. The dynamic
2634 object will contain position independent code, so all references
2635 from the dynamic object to this symbol will go through the global
2636 offset table. The dynamic linker will use the .dynsym entry to
2637 determine the address it must put in the global offset table, so
2638 both the dynamic object and the regular object will refer to the
2639 same memory location for the variable. */
2640
2641 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
2642 copy the initial value out of the dynamic object and into the
2643 runtime process image. */
2644 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2645 {
2646 s = htab->elf.sdynrelro;
2647 srel = htab->elf.sreldynrelro;
2648 }
2649 else
2650 {
2651 s = htab->elf.sdynbss;
2652 srel = htab->elf.srelbss;
2653 }
2654 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2655 {
2656 srel->size += sizeof (Elf32_External_Rel);
2657 h->needs_copy = 1;
2658 }
2659
2660 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2661 }
2662
2663 /* Allocate space in .plt, .got and associated reloc sections for
2664 dynamic relocs. */
2665
2666 static bfd_boolean
2667 elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2668 {
2669 struct bfd_link_info *info;
2670 struct elf_i386_link_hash_table *htab;
2671 struct elf_i386_link_hash_entry *eh;
2672 struct elf_dyn_relocs *p;
2673 unsigned plt_entry_size;
2674 bfd_boolean resolved_to_zero;
2675
2676 if (h->root.type == bfd_link_hash_indirect)
2677 return TRUE;
2678
2679 eh = (struct elf_i386_link_hash_entry *) h;
2680
2681 info = (struct bfd_link_info *) inf;
2682 htab = elf_i386_hash_table (info);
2683 if (htab == NULL)
2684 return FALSE;
2685
2686 plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
2687
2688 resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2689 eh->has_got_reloc,
2690 eh);
2691
2692 /* Clear the reference count of function pointer relocations if
2693 symbol isn't a normal function. */
2694 if (h->type != STT_FUNC)
2695 eh->func_pointer_refcount = 0;
2696
2697 /* We can't use the GOT PLT if pointer equality is needed since
2698 finish_dynamic_symbol won't clear symbol value and the dynamic
2699 linker won't update the GOT slot. We will get into an infinite
2700 loop at run-time. */
2701 if (htab->plt_got != NULL
2702 && h->type != STT_GNU_IFUNC
2703 && !h->pointer_equality_needed
2704 && h->plt.refcount > 0
2705 && h->got.refcount > 0)
2706 {
2707 /* Don't use the regular PLT if there are both GOT and GOTPLT
2708 reloctions. */
2709 h->plt.offset = (bfd_vma) -1;
2710
2711 /* Use the GOT PLT. */
2712 eh->plt_got.refcount = 1;
2713 }
2714
2715 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2716 here if it is defined and referenced in a non-shared object. */
2717 if (h->type == STT_GNU_IFUNC
2718 && h->def_regular)
2719 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
2720 &htab->readonly_dynrelocs_against_ifunc,
2721 plt_entry_size,
2722 plt_entry_size, 4, TRUE);
2723 /* Don't create the PLT entry if there are only function pointer
2724 relocations which can be resolved at run-time. */
2725 else if (htab->elf.dynamic_sections_created
2726 && (h->plt.refcount > eh->func_pointer_refcount
2727 || eh->plt_got.refcount > 0))
2728 {
2729 bfd_boolean use_plt_got;
2730
2731 /* Clear the reference count of function pointer relocations
2732 if PLT is used. */
2733 eh->func_pointer_refcount = 0;
2734
2735 if ((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
2736 {
2737 /* Don't use the regular PLT for DF_BIND_NOW. */
2738 h->plt.offset = (bfd_vma) -1;
2739
2740 /* Use the GOT PLT. */
2741 h->got.refcount = 1;
2742 eh->plt_got.refcount = 1;
2743 }
2744
2745 use_plt_got = eh->plt_got.refcount > 0;
2746
2747 /* Make sure this symbol is output as a dynamic symbol.
2748 Undefined weak syms won't yet be marked as dynamic. */
2749 if (h->dynindx == -1
2750 && !h->forced_local
2751 && !resolved_to_zero)
2752 {
2753 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2754 return FALSE;
2755 }
2756
2757 if (bfd_link_pic (info)
2758 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2759 {
2760 asection *s = htab->elf.splt;
2761 asection *got_s = htab->plt_got;
2762
2763 /* If this is the first .plt entry, make room for the special
2764 first entry. The .plt section is used by prelink to undo
2765 prelinking for dynamic relocations. */
2766 if (s->size == 0)
2767 s->size = plt_entry_size;
2768
2769 if (use_plt_got)
2770 eh->plt_got.offset = got_s->size;
2771 else
2772 h->plt.offset = s->size;
2773
2774 /* If this symbol is not defined in a regular file, and we are
2775 not generating a shared library, then set the symbol to this
2776 location in the .plt. This is required to make function
2777 pointers compare as equal between the normal executable and
2778 the shared library. */
2779 if (! bfd_link_pic (info)
2780 && !h->def_regular)
2781 {
2782 if (use_plt_got)
2783 {
2784 /* We need to make a call to the entry of the GOT PLT
2785 instead of regular PLT entry. */
2786 h->root.u.def.section = got_s;
2787 h->root.u.def.value = eh->plt_got.offset;
2788 }
2789 else
2790 {
2791 h->root.u.def.section = s;
2792 h->root.u.def.value = h->plt.offset;
2793 }
2794 }
2795
2796 /* Make room for this entry. */
2797 if (use_plt_got)
2798 got_s->size += sizeof (elf_i386_got_plt_entry);
2799 else
2800 {
2801 s->size += plt_entry_size;
2802
2803 /* We also need to make an entry in the .got.plt section,
2804 which will be placed in the .got section by the linker
2805 script. */
2806 htab->elf.sgotplt->size += 4;
2807
2808 /* There should be no PLT relocation against resolved
2809 undefined weak symbol in executable. */
2810 if (!resolved_to_zero)
2811 {
2812 /* We also need to make an entry in the .rel.plt
2813 section. */
2814 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2815 htab->elf.srelplt->reloc_count++;
2816 }
2817 }
2818
2819 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks
2820 && !bfd_link_pic (info))
2821 {
2822 /* VxWorks has a second set of relocations for each PLT entry
2823 in executables. They go in a separate relocation section,
2824 which is processed by the kernel loader. */
2825
2826 /* There are two relocations for the initial PLT entry: an
2827 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
2828 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
2829
2830 if (h->plt.offset == plt_entry_size)
2831 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2832
2833 /* There are two extra relocations for each subsequent PLT entry:
2834 an R_386_32 relocation for the GOT entry, and an R_386_32
2835 relocation for the PLT entry. */
2836
2837 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2838 }
2839 }
2840 else
2841 {
2842 eh->plt_got.offset = (bfd_vma) -1;
2843 h->plt.offset = (bfd_vma) -1;
2844 h->needs_plt = 0;
2845 }
2846 }
2847 else
2848 {
2849 eh->plt_got.offset = (bfd_vma) -1;
2850 h->plt.offset = (bfd_vma) -1;
2851 h->needs_plt = 0;
2852 }
2853
2854 eh->tlsdesc_got = (bfd_vma) -1;
2855
2856 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
2857 make it a R_386_TLS_LE_32 requiring no TLS entry. */
2858 if (h->got.refcount > 0
2859 && bfd_link_executable (info)
2860 && h->dynindx == -1
2861 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
2862 h->got.offset = (bfd_vma) -1;
2863 else if (h->got.refcount > 0)
2864 {
2865 asection *s;
2866 bfd_boolean dyn;
2867 int tls_type = elf_i386_hash_entry(h)->tls_type;
2868
2869 /* Make sure this symbol is output as a dynamic symbol.
2870 Undefined weak syms won't yet be marked as dynamic.
2871 PR ld/21402: If this symbol isn't undefined weak symbol,
2872 don't make it dynamic in PIE. */
2873 if (h->dynindx == -1
2874 && !h->forced_local
2875 && !resolved_to_zero
2876 && (h->root.type == bfd_link_hash_undefweak
2877 || !bfd_link_pie (info)))
2878 {
2879 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2880 return FALSE;
2881 }
2882
2883 s = htab->elf.sgot;
2884 if (GOT_TLS_GDESC_P (tls_type))
2885 {
2886 eh->tlsdesc_got = htab->elf.sgotplt->size
2887 - elf_i386_compute_jump_table_size (htab);
2888 htab->elf.sgotplt->size += 8;
2889 h->got.offset = (bfd_vma) -2;
2890 }
2891 if (! GOT_TLS_GDESC_P (tls_type)
2892 || GOT_TLS_GD_P (tls_type))
2893 {
2894 h->got.offset = s->size;
2895 s->size += 4;
2896 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
2897 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
2898 s->size += 4;
2899 }
2900 dyn = htab->elf.dynamic_sections_created;
2901 /* R_386_TLS_IE_32 needs one dynamic relocation,
2902 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
2903 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
2904 need two), R_386_TLS_GD needs one if local symbol and two if
2905 global. No dynamic relocation against resolved undefined weak
2906 symbol in executable. */
2907 if (tls_type == GOT_TLS_IE_BOTH)
2908 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2909 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2910 || (tls_type & GOT_TLS_IE))
2911 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2912 else if (GOT_TLS_GD_P (tls_type))
2913 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2914 else if (! GOT_TLS_GDESC_P (tls_type)
2915 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2916 && !resolved_to_zero)
2917 || h->root.type != bfd_link_hash_undefweak)
2918 && (bfd_link_pic (info)
2919 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2920 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2921 if (GOT_TLS_GDESC_P (tls_type))
2922 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2923 }
2924 else
2925 h->got.offset = (bfd_vma) -1;
2926
2927 if (eh->dyn_relocs == NULL)
2928 return TRUE;
2929
2930 /* In the shared -Bsymbolic case, discard space allocated for
2931 dynamic pc-relative relocs against symbols which turn out to be
2932 defined in regular objects. For the normal shared case, discard
2933 space for pc-relative relocs that have become local due to symbol
2934 visibility changes. */
2935
2936 if (bfd_link_pic (info))
2937 {
2938 /* The only reloc that uses pc_count is R_386_PC32, which will
2939 appear on a call or on something like ".long foo - .". We
2940 want calls to protected symbols to resolve directly to the
2941 function rather than going via the plt. If people want
2942 function pointer comparisons to work as expected then they
2943 should avoid writing assembly like ".long foo - .". */
2944 if (SYMBOL_CALLS_LOCAL (info, h))
2945 {
2946 struct elf_dyn_relocs **pp;
2947
2948 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2949 {
2950 p->count -= p->pc_count;
2951 p->pc_count = 0;
2952 if (p->count == 0)
2953 *pp = p->next;
2954 else
2955 pp = &p->next;
2956 }
2957 }
2958
2959 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
2960 {
2961 struct elf_dyn_relocs **pp;
2962 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2963 {
2964 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2965 *pp = p->next;
2966 else
2967 pp = &p->next;
2968 }
2969 }
2970
2971 /* Also discard relocs on undefined weak syms with non-default
2972 visibility or in PIE. */
2973 if (eh->dyn_relocs != NULL
2974 && h->root.type == bfd_link_hash_undefweak)
2975 {
2976 /* Undefined weak symbol is never bound locally in shared
2977 library. */
2978 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2979 || resolved_to_zero)
2980 {
2981 if (h->non_got_ref)
2982 {
2983 /* Keep dynamic non-GOT/non-PLT relocation so that we
2984 can branch to 0 without PLT. */
2985 struct elf_dyn_relocs **pp;
2986
2987 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2988 if (p->pc_count == 0)
2989 *pp = p->next;
2990 else
2991 {
2992 /* Remove non-R_386_PC32 relocation. */
2993 p->count = p->pc_count;
2994 pp = &p->next;
2995 }
2996
2997 if (eh->dyn_relocs != NULL)
2998 {
2999 /* Make sure undefined weak symbols are output
3000 as dynamic symbols in PIEs for dynamic non-GOT
3001 non-PLT reloations. */
3002 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3003 return FALSE;
3004 }
3005 }
3006 else
3007 eh->dyn_relocs = NULL;
3008 }
3009 else if (h->dynindx == -1
3010 && !h->forced_local)
3011 {
3012 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3013 return FALSE;
3014 }
3015 }
3016 }
3017 else if (ELIMINATE_COPY_RELOCS)
3018 {
3019 /* For the non-shared case, discard space for relocs against
3020 symbols which turn out to need copy relocs or are not
3021 dynamic. Keep dynamic relocations for run-time function
3022 pointer initialization. */
3023
3024 if ((!h->non_got_ref
3025 || eh->func_pointer_refcount > 0
3026 || (h->root.type == bfd_link_hash_undefweak
3027 && !resolved_to_zero))
3028 && ((h->def_dynamic
3029 && !h->def_regular)
3030 || (htab->elf.dynamic_sections_created
3031 && (h->root.type == bfd_link_hash_undefweak
3032 || h->root.type == bfd_link_hash_undefined))))
3033 {
3034 /* Make sure this symbol is output as a dynamic symbol.
3035 Undefined weak syms won't yet be marked as dynamic. */
3036 if (h->dynindx == -1
3037 && !h->forced_local
3038 && !resolved_to_zero)
3039 {
3040 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3041 return FALSE;
3042 }
3043
3044 /* If that succeeded, we know we'll be keeping all the
3045 relocs. */
3046 if (h->dynindx != -1)
3047 goto keep;
3048 }
3049
3050 eh->dyn_relocs = NULL;
3051 eh->func_pointer_refcount = 0;
3052
3053 keep: ;
3054 }
3055
3056 /* Finally, allocate space. */
3057 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3058 {
3059 asection *sreloc;
3060
3061 sreloc = elf_section_data (p->sec)->sreloc;
3062
3063 BFD_ASSERT (sreloc != NULL);
3064 sreloc->size += p->count * sizeof (Elf32_External_Rel);
3065 }
3066
3067 return TRUE;
3068 }
3069
3070 /* Allocate space in .plt, .got and associated reloc sections for
3071 local dynamic relocs. */
3072
3073 static bfd_boolean
3074 elf_i386_allocate_local_dynrelocs (void **slot, void *inf)
3075 {
3076 struct elf_link_hash_entry *h
3077 = (struct elf_link_hash_entry *) *slot;
3078
3079 if (h->type != STT_GNU_IFUNC
3080 || !h->def_regular
3081 || !h->ref_regular
3082 || !h->forced_local
3083 || h->root.type != bfd_link_hash_defined)
3084 abort ();
3085
3086 return elf_i386_allocate_dynrelocs (h, inf);
3087 }
3088
3089 /* Find any dynamic relocs that apply to read-only sections. */
3090
3091 static bfd_boolean
3092 elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3093 {
3094 struct elf_i386_link_hash_entry *eh;
3095 struct elf_dyn_relocs *p;
3096
3097 /* Skip local IFUNC symbols. */
3098 if (h->forced_local && h->type == STT_GNU_IFUNC)
3099 return TRUE;
3100
3101 eh = (struct elf_i386_link_hash_entry *) h;
3102 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3103 {
3104 asection *s = p->sec->output_section;
3105
3106 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3107 {
3108 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3109
3110 info->flags |= DF_TEXTREL;
3111
3112 if ((info->warn_shared_textrel && bfd_link_pic (info))
3113 || info->error_textrel)
3114 /* xgettext:c-format */
3115 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
3116 p->sec->owner, h->root.root.string,
3117 p->sec);
3118
3119 /* Not an error, just cut short the traversal. */
3120 return FALSE;
3121 }
3122 }
3123 return TRUE;
3124 }
3125
3126 /* Convert load via the GOT slot to load immediate. */
3127
3128 static bfd_boolean
3129 elf_i386_convert_load (bfd *abfd, asection *sec,
3130 struct bfd_link_info *link_info)
3131 {
3132 struct elf_i386_link_hash_table *htab;
3133 Elf_Internal_Shdr *symtab_hdr;
3134 Elf_Internal_Rela *internal_relocs;
3135 Elf_Internal_Rela *irel, *irelend;
3136 bfd_byte *contents;
3137 bfd_boolean changed;
3138 bfd_signed_vma *local_got_refcounts;
3139
3140 /* Don't even try to convert non-ELF outputs. */
3141 if (!is_elf_hash_table (link_info->hash))
3142 return FALSE;
3143
3144 /* Nothing to do if there is no need or no output. */
3145 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
3146 || sec->need_convert_load == 0
3147 || bfd_is_abs_section (sec->output_section))
3148 return TRUE;
3149
3150 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3151
3152 /* Load the relocations for this section. */
3153 internal_relocs = (_bfd_elf_link_read_relocs
3154 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
3155 link_info->keep_memory));
3156 if (internal_relocs == NULL)
3157 return FALSE;
3158
3159 changed = FALSE;
3160 htab = elf_i386_hash_table (link_info);
3161 local_got_refcounts = elf_local_got_refcounts (abfd);
3162
3163 /* Get the section contents. */
3164 if (elf_section_data (sec)->this_hdr.contents != NULL)
3165 contents = elf_section_data (sec)->this_hdr.contents;
3166 else
3167 {
3168 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3169 goto error_return;
3170 }
3171
3172 irelend = internal_relocs + sec->reloc_count;
3173 for (irel = internal_relocs; irel < irelend; irel++)
3174 {
3175 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
3176 unsigned int r_symndx;
3177 struct elf_link_hash_entry *h;
3178 bfd_boolean converted;
3179
3180 /* Don't convert R_386_GOT32 since we can't tell if it is applied
3181 to "mov $foo@GOT, %reg" which isn't a load via GOT. */
3182 if (r_type != R_386_GOT32X)
3183 continue;
3184
3185 r_symndx = ELF32_R_SYM (irel->r_info);
3186 if (r_symndx < symtab_hdr->sh_info)
3187 h = elf_i386_get_local_sym_hash (htab, sec->owner,
3188 (const Elf_Internal_Rela *) irel,
3189 FALSE);
3190 else
3191 {
3192 h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
3193 while (h->root.type == bfd_link_hash_indirect
3194 || h->root.type == bfd_link_hash_warning)
3195 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3196 }
3197
3198 /* STT_GNU_IFUNC must keep GOT32 relocations. */
3199 if (h != NULL && h->type == STT_GNU_IFUNC)
3200 continue;
3201
3202 converted = FALSE;
3203 if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
3204 irel, h, &converted, link_info))
3205 goto error_return;
3206
3207 if (converted)
3208 {
3209 changed = converted;
3210 if (h)
3211 {
3212 if (h->got.refcount > 0)
3213 h->got.refcount -= 1;
3214 }
3215 else
3216 {
3217 if (local_got_refcounts != NULL
3218 && local_got_refcounts[r_symndx] > 0)
3219 local_got_refcounts[r_symndx] -= 1;
3220 }
3221 }
3222 }
3223
3224 if (contents != NULL
3225 && elf_section_data (sec)->this_hdr.contents != contents)
3226 {
3227 if (!changed && !link_info->keep_memory)
3228 free (contents);
3229 else
3230 {
3231 /* Cache the section contents for elf_link_input_bfd. */
3232 elf_section_data (sec)->this_hdr.contents = contents;
3233 }
3234 }
3235
3236 if (elf_section_data (sec)->relocs != internal_relocs)
3237 {
3238 if (!changed)
3239 free (internal_relocs);
3240 else
3241 elf_section_data (sec)->relocs = internal_relocs;
3242 }
3243
3244 return TRUE;
3245
3246 error_return:
3247 if (contents != NULL
3248 && elf_section_data (sec)->this_hdr.contents != contents)
3249 free (contents);
3250 if (internal_relocs != NULL
3251 && elf_section_data (sec)->relocs != internal_relocs)
3252 free (internal_relocs);
3253 return FALSE;
3254 }
3255
3256 /* Set the sizes of the dynamic sections. */
3257
3258 static bfd_boolean
3259 elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3260 {
3261 struct elf_i386_link_hash_table *htab;
3262 bfd *dynobj;
3263 asection *s;
3264 bfd_boolean relocs;
3265 bfd *ibfd;
3266
3267 htab = elf_i386_hash_table (info);
3268 if (htab == NULL)
3269 return FALSE;
3270 dynobj = htab->elf.dynobj;
3271 if (dynobj == NULL)
3272 abort ();
3273
3274 /* Set up .got offsets for local syms, and space for local dynamic
3275 relocs. */
3276 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3277 {
3278 bfd_signed_vma *local_got;
3279 bfd_signed_vma *end_local_got;
3280 char *local_tls_type;
3281 bfd_vma *local_tlsdesc_gotent;
3282 bfd_size_type locsymcount;
3283 Elf_Internal_Shdr *symtab_hdr;
3284 asection *srel;
3285
3286 if (! is_i386_elf (ibfd))
3287 continue;
3288
3289 for (s = ibfd->sections; s != NULL; s = s->next)
3290 {
3291 struct elf_dyn_relocs *p;
3292
3293 if (!elf_i386_convert_load (ibfd, s, info))
3294 return FALSE;
3295
3296 for (p = ((struct elf_dyn_relocs *)
3297 elf_section_data (s)->local_dynrel);
3298 p != NULL;
3299 p = p->next)
3300 {
3301 if (!bfd_is_abs_section (p->sec)
3302 && bfd_is_abs_section (p->sec->output_section))
3303 {
3304 /* Input section has been discarded, either because
3305 it is a copy of a linkonce section or due to
3306 linker script /DISCARD/, so we'll be discarding
3307 the relocs too. */
3308 }
3309 else if (get_elf_i386_backend_data (output_bfd)->is_vxworks
3310 && strcmp (p->sec->output_section->name,
3311 ".tls_vars") == 0)
3312 {
3313 /* Relocations in vxworks .tls_vars sections are
3314 handled specially by the loader. */
3315 }
3316 else if (p->count != 0)
3317 {
3318 srel = elf_section_data (p->sec)->sreloc;
3319 srel->size += p->count * sizeof (Elf32_External_Rel);
3320 if ((p->sec->output_section->flags & SEC_READONLY) != 0
3321 && (info->flags & DF_TEXTREL) == 0)
3322 {
3323 info->flags |= DF_TEXTREL;
3324 if ((info->warn_shared_textrel && bfd_link_pic (info))
3325 || info->error_textrel)
3326 /* xgettext:c-format */
3327 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
3328 p->sec->owner, p->sec);
3329 }
3330 }
3331 }
3332 }
3333
3334 local_got = elf_local_got_refcounts (ibfd);
3335 if (!local_got)
3336 continue;
3337
3338 symtab_hdr = &elf_symtab_hdr (ibfd);
3339 locsymcount = symtab_hdr->sh_info;
3340 end_local_got = local_got + locsymcount;
3341 local_tls_type = elf_i386_local_got_tls_type (ibfd);
3342 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
3343 s = htab->elf.sgot;
3344 srel = htab->elf.srelgot;
3345 for (; local_got < end_local_got;
3346 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
3347 {
3348 *local_tlsdesc_gotent = (bfd_vma) -1;
3349 if (*local_got > 0)
3350 {
3351 if (GOT_TLS_GDESC_P (*local_tls_type))
3352 {
3353 *local_tlsdesc_gotent = htab->elf.sgotplt->size
3354 - elf_i386_compute_jump_table_size (htab);
3355 htab->elf.sgotplt->size += 8;
3356 *local_got = (bfd_vma) -2;
3357 }
3358 if (! GOT_TLS_GDESC_P (*local_tls_type)
3359 || GOT_TLS_GD_P (*local_tls_type))
3360 {
3361 *local_got = s->size;
3362 s->size += 4;
3363 if (GOT_TLS_GD_P (*local_tls_type)
3364 || *local_tls_type == GOT_TLS_IE_BOTH)
3365 s->size += 4;
3366 }
3367 if (bfd_link_pic (info)
3368 || GOT_TLS_GD_ANY_P (*local_tls_type)
3369 || (*local_tls_type & GOT_TLS_IE))
3370 {
3371 if (*local_tls_type == GOT_TLS_IE_BOTH)
3372 srel->size += 2 * sizeof (Elf32_External_Rel);
3373 else if (GOT_TLS_GD_P (*local_tls_type)
3374 || ! GOT_TLS_GDESC_P (*local_tls_type))
3375 srel->size += sizeof (Elf32_External_Rel);
3376 if (GOT_TLS_GDESC_P (*local_tls_type))
3377 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
3378 }
3379 }
3380 else
3381 *local_got = (bfd_vma) -1;
3382 }
3383 }
3384
3385 if (htab->tls_ldm_got.refcount > 0)
3386 {
3387 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
3388 relocs. */
3389 htab->tls_ldm_got.offset = htab->elf.sgot->size;
3390 htab->elf.sgot->size += 8;
3391 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
3392 }
3393 else
3394 htab->tls_ldm_got.offset = -1;
3395
3396 /* Allocate global sym .plt and .got entries, and space for global
3397 sym dynamic relocs. */
3398 elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
3399
3400 /* Allocate .plt and .got entries, and space for local symbols. */
3401 htab_traverse (htab->loc_hash_table,
3402 elf_i386_allocate_local_dynrelocs,
3403 info);
3404
3405 /* For every jump slot reserved in the sgotplt, reloc_count is
3406 incremented. However, when we reserve space for TLS descriptors,
3407 it's not incremented, so in order to compute the space reserved
3408 for them, it suffices to multiply the reloc count by the jump
3409 slot size.
3410
3411 PR ld/13302: We start next_irelative_index at the end of .rela.plt
3412 so that R_386_IRELATIVE entries come last. */
3413 if (htab->elf.srelplt)
3414 {
3415 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
3416 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
3417 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
3418 }
3419 else if (htab->elf.irelplt)
3420 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
3421
3422
3423 if (htab->elf.sgotplt)
3424 {
3425 /* Don't allocate .got.plt section if there are no GOT nor PLT
3426 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
3427 if ((htab->elf.hgot == NULL
3428 || !htab->elf.hgot->ref_regular_nonweak)
3429 && (htab->elf.sgotplt->size
3430 == get_elf_backend_data (output_bfd)->got_header_size)
3431 && (htab->elf.splt == NULL
3432 || htab->elf.splt->size == 0)
3433 && (htab->elf.sgot == NULL
3434 || htab->elf.sgot->size == 0)
3435 && (htab->elf.iplt == NULL
3436 || htab->elf.iplt->size == 0)
3437 && (htab->elf.igotplt == NULL
3438 || htab->elf.igotplt->size == 0))
3439 htab->elf.sgotplt->size = 0;
3440 }
3441
3442 if (_bfd_elf_eh_frame_present (info))
3443 {
3444 if (htab->plt_eh_frame != NULL
3445 && htab->elf.splt != NULL
3446 && htab->elf.splt->size != 0
3447 && !bfd_is_abs_section (htab->elf.splt->output_section))
3448 htab->plt_eh_frame->size
3449 = get_elf_i386_backend_data (output_bfd)->plt->eh_frame_plt_size;
3450
3451 if (htab->plt_got_eh_frame != NULL
3452 && htab->plt_got != NULL
3453 && htab->plt_got->size != 0
3454 && !bfd_is_abs_section (htab->plt_got->output_section))
3455 htab->plt_got_eh_frame->size
3456 = get_elf_i386_backend_data (output_bfd)->plt->eh_frame_plt_got_size;
3457 }
3458
3459 /* We now have determined the sizes of the various dynamic sections.
3460 Allocate memory for them. */
3461 relocs = FALSE;
3462 for (s = dynobj->sections; s != NULL; s = s->next)
3463 {
3464 bfd_boolean strip_section = TRUE;
3465
3466 if ((s->flags & SEC_LINKER_CREATED) == 0)
3467 continue;
3468
3469 if (s == htab->elf.splt
3470 || s == htab->elf.sgot)
3471 {
3472 /* Strip this section if we don't need it; see the
3473 comment below. */
3474 /* We'd like to strip these sections if they aren't needed, but if
3475 we've exported dynamic symbols from them we must leave them.
3476 It's too late to tell BFD to get rid of the symbols. */
3477
3478 if (htab->elf.hplt != NULL)
3479 strip_section = FALSE;
3480 }
3481 else if (s == htab->elf.sgotplt
3482 || s == htab->elf.iplt
3483 || s == htab->elf.igotplt
3484 || s == htab->plt_got
3485 || s == htab->plt_eh_frame
3486 || s == htab->plt_got_eh_frame
3487 || s == htab->elf.sdynbss
3488 || s == htab->elf.sdynrelro)
3489 {
3490 /* Strip these too. */
3491 }
3492 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
3493 {
3494 if (s->size != 0
3495 && s != htab->elf.srelplt
3496 && s != htab->srelplt2)
3497 relocs = TRUE;
3498
3499 /* We use the reloc_count field as a counter if we need
3500 to copy relocs into the output file. */
3501 s->reloc_count = 0;
3502 }
3503 else
3504 {
3505 /* It's not one of our sections, so don't allocate space. */
3506 continue;
3507 }
3508
3509 if (s->size == 0)
3510 {
3511 /* If we don't need this section, strip it from the
3512 output file. This is mostly to handle .rel.bss and
3513 .rel.plt. We must create both sections in
3514 create_dynamic_sections, because they must be created
3515 before the linker maps input sections to output
3516 sections. The linker does that before
3517 adjust_dynamic_symbol is called, and it is that
3518 function which decides whether anything needs to go
3519 into these sections. */
3520 if (strip_section)
3521 s->flags |= SEC_EXCLUDE;
3522 continue;
3523 }
3524
3525 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3526 continue;
3527
3528 /* Allocate memory for the section contents. We use bfd_zalloc
3529 here in case unused entries are not reclaimed before the
3530 section's contents are written out. This should not happen,
3531 but this way if it does, we get a R_386_NONE reloc instead
3532 of garbage. */
3533 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
3534 if (s->contents == NULL)
3535 return FALSE;
3536 }
3537
3538 if (htab->plt_eh_frame != NULL
3539 && htab->plt_eh_frame->contents != NULL)
3540 {
3541 memcpy (htab->plt_eh_frame->contents,
3542 get_elf_i386_backend_data (output_bfd)->plt->eh_frame_plt,
3543 htab->plt_eh_frame->size);
3544 bfd_put_32 (dynobj, htab->elf.splt->size,
3545 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3546 }
3547
3548 if (htab->plt_got_eh_frame != NULL
3549 && htab->plt_got_eh_frame->contents != NULL)
3550 {
3551 memcpy (htab->plt_got_eh_frame->contents,
3552 get_elf_i386_backend_data (output_bfd)->plt->eh_frame_plt_got,
3553 htab->plt_got_eh_frame->size);
3554 bfd_put_32 (dynobj, htab->plt_got->size,
3555 (htab->plt_got_eh_frame->contents
3556 + PLT_FDE_LEN_OFFSET));
3557 }
3558
3559 if (htab->elf.dynamic_sections_created)
3560 {
3561 /* Add some entries to the .dynamic section. We fill in the
3562 values later, in elf_i386_finish_dynamic_sections, but we
3563 must add the entries now so that we get the correct size for
3564 the .dynamic section. The DT_DEBUG entry is filled in by the
3565 dynamic linker and used by the debugger. */
3566 #define add_dynamic_entry(TAG, VAL) \
3567 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3568
3569 if (bfd_link_executable (info))
3570 {
3571 if (!add_dynamic_entry (DT_DEBUG, 0))
3572 return FALSE;
3573 }
3574
3575 if (htab->elf.splt->size != 0)
3576 {
3577 /* DT_PLTGOT is used by prelink even if there is no PLT
3578 relocation. */
3579 if (!add_dynamic_entry (DT_PLTGOT, 0))
3580 return FALSE;
3581
3582 if (htab->elf.srelplt->size != 0)
3583 {
3584 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3585 || !add_dynamic_entry (DT_PLTREL, DT_REL)
3586 || !add_dynamic_entry (DT_JMPREL, 0))
3587 return FALSE;
3588 }
3589 }
3590
3591 if (relocs)
3592 {
3593 if (!add_dynamic_entry (DT_REL, 0)
3594 || !add_dynamic_entry (DT_RELSZ, 0)
3595 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
3596 return FALSE;
3597
3598 /* If any dynamic relocs apply to a read-only section,
3599 then we need a DT_TEXTREL entry. */
3600 if ((info->flags & DF_TEXTREL) == 0)
3601 elf_link_hash_traverse (&htab->elf,
3602 elf_i386_readonly_dynrelocs, info);
3603
3604 if ((info->flags & DF_TEXTREL) != 0)
3605 {
3606 if (htab->readonly_dynrelocs_against_ifunc)
3607 {
3608 info->callbacks->einfo
3609 (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
3610 bfd_set_error (bfd_error_bad_value);
3611 return FALSE;
3612 }
3613
3614 if (!add_dynamic_entry (DT_TEXTREL, 0))
3615 return FALSE;
3616 }
3617 }
3618 if (get_elf_i386_backend_data (output_bfd)->is_vxworks
3619 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3620 return FALSE;
3621 }
3622 #undef add_dynamic_entry
3623
3624 return TRUE;
3625 }
3626
3627 static bfd_boolean
3628 elf_i386_always_size_sections (bfd *output_bfd,
3629 struct bfd_link_info *info)
3630 {
3631 asection *tls_sec = elf_hash_table (info)->tls_sec;
3632
3633 if (tls_sec)
3634 {
3635 struct elf_link_hash_entry *tlsbase;
3636
3637 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3638 "_TLS_MODULE_BASE_",
3639 FALSE, FALSE, FALSE);
3640
3641 if (tlsbase && tlsbase->type == STT_TLS)
3642 {
3643 struct elf_i386_link_hash_table *htab;
3644 struct bfd_link_hash_entry *bh = NULL;
3645 const struct elf_backend_data *bed
3646 = get_elf_backend_data (output_bfd);
3647
3648 htab = elf_i386_hash_table (info);
3649 if (htab == NULL)
3650 return FALSE;
3651
3652 if (!(_bfd_generic_link_add_one_symbol
3653 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3654 tls_sec, 0, NULL, FALSE,
3655 bed->collect, &bh)))
3656 return FALSE;
3657
3658 htab->tls_module_base = bh;
3659
3660 tlsbase = (struct elf_link_hash_entry *)bh;
3661 tlsbase->def_regular = 1;
3662 tlsbase->other = STV_HIDDEN;
3663 tlsbase->root.linker_def = 1;
3664 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
3665 }
3666 }
3667
3668 return TRUE;
3669 }
3670
3671 /* Set the correct type for an x86 ELF section. We do this by the
3672 section name, which is a hack, but ought to work. */
3673
3674 static bfd_boolean
3675 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
3676 Elf_Internal_Shdr *hdr,
3677 asection *sec)
3678 {
3679 const char *name;
3680
3681 name = bfd_get_section_name (abfd, sec);
3682
3683 /* This is an ugly, but unfortunately necessary hack that is
3684 needed when producing EFI binaries on x86. It tells
3685 elf.c:elf_fake_sections() not to consider ".reloc" as a section
3686 containing ELF relocation info. We need this hack in order to
3687 be able to generate ELF binaries that can be translated into
3688 EFI applications (which are essentially COFF objects). Those
3689 files contain a COFF ".reloc" section inside an ELFNN object,
3690 which would normally cause BFD to segfault because it would
3691 attempt to interpret this section as containing relocation
3692 entries for section "oc". With this hack enabled, ".reloc"
3693 will be treated as a normal data section, which will avoid the
3694 segfault. However, you won't be able to create an ELFNN binary
3695 with a section named "oc" that needs relocations, but that's
3696 the kind of ugly side-effects you get when detecting section
3697 types based on their names... In practice, this limitation is
3698 unlikely to bite. */
3699 if (strcmp (name, ".reloc") == 0)
3700 hdr->sh_type = SHT_PROGBITS;
3701
3702 return TRUE;
3703 }
3704
3705 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
3706 executables. Rather than setting it to the beginning of the TLS
3707 section, we have to set it to the end. This function may be called
3708 multiple times, it is idempotent. */
3709
3710 static void
3711 elf_i386_set_tls_module_base (struct bfd_link_info *info)
3712 {
3713 struct elf_i386_link_hash_table *htab;
3714 struct bfd_link_hash_entry *base;
3715
3716 if (!bfd_link_executable (info))
3717 return;
3718
3719 htab = elf_i386_hash_table (info);
3720 if (htab == NULL)
3721 return;
3722
3723 base = htab->tls_module_base;
3724 if (base == NULL)
3725 return;
3726
3727 base->u.def.value = htab->elf.tls_size;
3728 }
3729
3730 /* Return the base VMA address which should be subtracted from real addresses
3731 when resolving @dtpoff relocation.
3732 This is PT_TLS segment p_vaddr. */
3733
3734 static bfd_vma
3735 elf_i386_dtpoff_base (struct bfd_link_info *info)
3736 {
3737 /* If tls_sec is NULL, we should have signalled an error already. */
3738 if (elf_hash_table (info)->tls_sec == NULL)
3739 return 0;
3740 return elf_hash_table (info)->tls_sec->vma;
3741 }
3742
3743 /* Return the relocation value for @tpoff relocation
3744 if STT_TLS virtual address is ADDRESS. */
3745
3746 static bfd_vma
3747 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
3748 {
3749 struct elf_link_hash_table *htab = elf_hash_table (info);
3750 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3751 bfd_vma static_tls_size;
3752
3753 /* If tls_sec is NULL, we should have signalled an error already. */
3754 if (htab->tls_sec == NULL)
3755 return 0;
3756
3757 /* Consider special static TLS alignment requirements. */
3758 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3759 return static_tls_size + htab->tls_sec->vma - address;
3760 }
3761
3762 /* Relocate an i386 ELF section. */
3763
3764 static bfd_boolean
3765 elf_i386_relocate_section (bfd *output_bfd,
3766 struct bfd_link_info *info,
3767 bfd *input_bfd,
3768 asection *input_section,
3769 bfd_byte *contents,
3770 Elf_Internal_Rela *relocs,
3771 Elf_Internal_Sym *local_syms,
3772 asection **local_sections)
3773 {
3774 struct elf_i386_link_hash_table *htab;
3775 Elf_Internal_Shdr *symtab_hdr;
3776 struct elf_link_hash_entry **sym_hashes;
3777 bfd_vma *local_got_offsets;
3778 bfd_vma *local_tlsdesc_gotents;
3779 Elf_Internal_Rela *rel;
3780 Elf_Internal_Rela *wrel;
3781 Elf_Internal_Rela *relend;
3782 bfd_boolean is_vxworks_tls;
3783 unsigned plt_entry_size;
3784
3785 BFD_ASSERT (is_i386_elf (input_bfd));
3786
3787 /* Skip if check_relocs failed. */
3788 if (input_section->check_relocs_failed)
3789 return FALSE;
3790
3791 htab = elf_i386_hash_table (info);
3792 if (htab == NULL)
3793 return FALSE;
3794 symtab_hdr = &elf_symtab_hdr (input_bfd);
3795 sym_hashes = elf_sym_hashes (input_bfd);
3796 local_got_offsets = elf_local_got_offsets (input_bfd);
3797 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
3798 /* We have to handle relocations in vxworks .tls_vars sections
3799 specially, because the dynamic loader is 'weird'. */
3800 is_vxworks_tls = (get_elf_i386_backend_data (output_bfd)->is_vxworks
3801 && bfd_link_pic (info)
3802 && !strcmp (input_section->output_section->name,
3803 ".tls_vars"));
3804
3805 elf_i386_set_tls_module_base (info);
3806
3807 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
3808
3809 rel = wrel = relocs;
3810 relend = relocs + input_section->reloc_count;
3811 for (; rel < relend; wrel++, rel++)
3812 {
3813 unsigned int r_type;
3814 reloc_howto_type *howto;
3815 unsigned long r_symndx;
3816 struct elf_link_hash_entry *h;
3817 struct elf_i386_link_hash_entry *eh;
3818 Elf_Internal_Sym *sym;
3819 asection *sec;
3820 bfd_vma off, offplt, plt_offset;
3821 bfd_vma relocation;
3822 bfd_boolean unresolved_reloc;
3823 bfd_reloc_status_type r;
3824 unsigned int indx;
3825 int tls_type;
3826 bfd_vma st_size;
3827 asection *resolved_plt;
3828 bfd_boolean resolved_to_zero;
3829 bfd_boolean relative_reloc;
3830
3831 r_type = ELF32_R_TYPE (rel->r_info);
3832 if (r_type == R_386_GNU_VTINHERIT
3833 || r_type == R_386_GNU_VTENTRY)
3834 {
3835 if (wrel != rel)
3836 *wrel = *rel;
3837 continue;
3838 }
3839
3840 if ((indx = r_type) >= R_386_standard
3841 && ((indx = r_type - R_386_ext_offset) - R_386_standard
3842 >= R_386_ext - R_386_standard)
3843 && ((indx = r_type - R_386_tls_offset) - R_386_ext
3844 >= R_386_ext2 - R_386_ext))
3845 {
3846 _bfd_error_handler
3847 /* xgettext:c-format */
3848 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
3849 input_bfd, r_type, input_section);
3850 bfd_set_error (bfd_error_bad_value);
3851 return FALSE;
3852 }
3853 howto = elf_howto_table + indx;
3854
3855 r_symndx = ELF32_R_SYM (rel->r_info);
3856 h = NULL;
3857 sym = NULL;
3858 sec = NULL;
3859 unresolved_reloc = FALSE;
3860 if (r_symndx < symtab_hdr->sh_info)
3861 {
3862 sym = local_syms + r_symndx;
3863 sec = local_sections[r_symndx];
3864 relocation = (sec->output_section->vma
3865 + sec->output_offset
3866 + sym->st_value);
3867 st_size = sym->st_size;
3868
3869 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
3870 && ((sec->flags & SEC_MERGE) != 0
3871 || (bfd_link_relocatable (info)
3872 && sec->output_offset != 0)))
3873 {
3874 bfd_vma addend;
3875 bfd_byte *where = contents + rel->r_offset;
3876
3877 switch (howto->size)
3878 {
3879 case 0:
3880 addend = bfd_get_8 (input_bfd, where);
3881 if (howto->pc_relative)
3882 {
3883 addend = (addend ^ 0x80) - 0x80;
3884 addend += 1;
3885 }
3886 break;
3887 case 1:
3888 addend = bfd_get_16 (input_bfd, where);
3889 if (howto->pc_relative)
3890 {
3891 addend = (addend ^ 0x8000) - 0x8000;
3892 addend += 2;
3893 }
3894 break;
3895 case 2:
3896 addend = bfd_get_32 (input_bfd, where);
3897 if (howto->pc_relative)
3898 {
3899 addend = (addend ^ 0x80000000) - 0x80000000;
3900 addend += 4;
3901 }
3902 break;
3903 default:
3904 abort ();
3905 }
3906
3907 if (bfd_link_relocatable (info))
3908 addend += sec->output_offset;
3909 else
3910 {
3911 asection *msec = sec;
3912 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
3913 addend);
3914 addend -= relocation;
3915 addend += msec->output_section->vma + msec->output_offset;
3916 }
3917
3918 switch (howto->size)
3919 {
3920 case 0:
3921 /* FIXME: overflow checks. */
3922 if (howto->pc_relative)
3923 addend -= 1;
3924 bfd_put_8 (input_bfd, addend, where);
3925 break;
3926 case 1:
3927 if (howto->pc_relative)
3928 addend -= 2;
3929 bfd_put_16 (input_bfd, addend, where);
3930 break;
3931 case 2:
3932 if (howto->pc_relative)
3933 addend -= 4;
3934 bfd_put_32 (input_bfd, addend, where);
3935 break;
3936 }
3937 }
3938 else if (!bfd_link_relocatable (info)
3939 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3940 {
3941 /* Relocate against local STT_GNU_IFUNC symbol. */
3942 h = elf_i386_get_local_sym_hash (htab, input_bfd, rel,
3943 FALSE);
3944 if (h == NULL)
3945 abort ();
3946
3947 /* Set STT_GNU_IFUNC symbol value. */
3948 h->root.u.def.value = sym->st_value;
3949 h->root.u.def.section = sec;
3950 }
3951 }
3952 else
3953 {
3954 bfd_boolean warned ATTRIBUTE_UNUSED;
3955 bfd_boolean ignored ATTRIBUTE_UNUSED;
3956
3957 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3958 r_symndx, symtab_hdr, sym_hashes,
3959 h, sec, relocation,
3960 unresolved_reloc, warned, ignored);
3961 st_size = h->size;
3962 }
3963
3964 if (sec != NULL && discarded_section (sec))
3965 {
3966 _bfd_clear_contents (howto, input_bfd, input_section,
3967 contents + rel->r_offset);
3968 wrel->r_offset = rel->r_offset;
3969 wrel->r_info = 0;
3970 wrel->r_addend = 0;
3971
3972 /* For ld -r, remove relocations in debug sections against
3973 sections defined in discarded sections. Not done for
3974 eh_frame editing code expects to be present. */
3975 if (bfd_link_relocatable (info)
3976 && (input_section->flags & SEC_DEBUGGING))
3977 wrel--;
3978
3979 continue;
3980 }
3981
3982 if (bfd_link_relocatable (info))
3983 {
3984 if (wrel != rel)
3985 *wrel = *rel;
3986 continue;
3987 }
3988
3989 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3990 it here if it is defined in a non-shared object. */
3991 if (h != NULL
3992 && h->type == STT_GNU_IFUNC
3993 && h->def_regular)
3994 {
3995 asection *plt, *gotplt, *base_got;
3996 bfd_vma plt_index;
3997 const char *name;
3998
3999 if ((input_section->flags & SEC_ALLOC) == 0)
4000 {
4001 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4002 sections because such sections are not SEC_ALLOC and
4003 thus ld.so will not process them. */
4004 if ((input_section->flags & SEC_DEBUGGING) != 0)
4005 continue;
4006 abort ();
4007 }
4008
4009 /* STT_GNU_IFUNC symbol must go through PLT. */
4010 if (htab->elf.splt != NULL)
4011 {
4012 plt = htab->elf.splt;
4013 gotplt = htab->elf.sgotplt;
4014 }
4015 else
4016 {
4017 plt = htab->elf.iplt;
4018 gotplt = htab->elf.igotplt;
4019 }
4020
4021 switch (r_type)
4022 {
4023 default:
4024 break;
4025
4026 case R_386_GOT32:
4027 case R_386_GOT32X:
4028 base_got = htab->elf.sgot;
4029 off = h->got.offset;
4030
4031 if (base_got == NULL)
4032 abort ();
4033
4034 if (off == (bfd_vma) -1)
4035 {
4036 /* We can't use h->got.offset here to save state, or
4037 even just remember the offset, as finish_dynamic_symbol
4038 would use that as offset into .got. */
4039
4040 if (h->plt.offset == (bfd_vma) -1)
4041 abort ();
4042
4043 if (htab->elf.splt != NULL)
4044 {
4045 plt_index = h->plt.offset / plt_entry_size - 1;
4046 off = (plt_index + 3) * 4;
4047 base_got = htab->elf.sgotplt;
4048 }
4049 else
4050 {
4051 plt_index = h->plt.offset / plt_entry_size;
4052 off = plt_index * 4;
4053 base_got = htab->elf.igotplt;
4054 }
4055
4056 if (h->dynindx == -1
4057 || h->forced_local
4058 || info->symbolic)
4059 {
4060 /* This references the local defitionion. We must
4061 initialize this entry in the global offset table.
4062 Since the offset must always be a multiple of 8,
4063 we use the least significant bit to record
4064 whether we have initialized it already.
4065
4066 When doing a dynamic link, we create a .rela.got
4067 relocation entry to initialize the value. This
4068 is done in the finish_dynamic_symbol routine. */
4069 if ((off & 1) != 0)
4070 off &= ~1;
4071 else
4072 {
4073 bfd_put_32 (output_bfd, relocation,
4074 base_got->contents + off);
4075 h->got.offset |= 1;
4076 }
4077 }
4078
4079 relocation = off;
4080 }
4081 else
4082 relocation = (base_got->output_section->vma
4083 + base_got->output_offset + off
4084 - gotplt->output_section->vma
4085 - gotplt->output_offset);
4086
4087 if (rel->r_offset > 1
4088 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
4089 && *(contents + rel->r_offset - 2) != 0x8d)
4090 {
4091 if (bfd_link_pic (info))
4092 goto disallow_got32;
4093
4094 /* Add the GOT base if there is no base register. */
4095 relocation += (gotplt->output_section->vma
4096 + gotplt->output_offset);
4097 }
4098 else if (htab->elf.splt == NULL)
4099 {
4100 /* Adjust for static executables. */
4101 relocation += gotplt->output_offset;
4102 }
4103
4104 goto do_relocation;
4105 }
4106
4107 if (h->plt.offset == (bfd_vma) -1)
4108 {
4109 /* Handle static pointers of STT_GNU_IFUNC symbols. */
4110 if (r_type == R_386_32
4111 && (input_section->flags & SEC_CODE) == 0)
4112 goto do_ifunc_pointer;
4113 goto bad_ifunc_reloc;
4114 }
4115
4116 relocation = (plt->output_section->vma
4117 + plt->output_offset + h->plt.offset);
4118
4119 switch (r_type)
4120 {
4121 default:
4122 bad_ifunc_reloc:
4123 if (h->root.root.string)
4124 name = h->root.root.string;
4125 else
4126 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4127 NULL);
4128 _bfd_error_handler
4129 /* xgettext:c-format */
4130 (_("%B: relocation %s against STT_GNU_IFUNC "
4131 "symbol `%s' isn't supported"), input_bfd,
4132 howto->name, name);
4133 bfd_set_error (bfd_error_bad_value);
4134 return FALSE;
4135
4136 case R_386_32:
4137 /* Generate dynamic relcoation only when there is a
4138 non-GOT reference in a shared object. */
4139 if ((bfd_link_pic (info) && h->non_got_ref)
4140 || h->plt.offset == (bfd_vma) -1)
4141 {
4142 Elf_Internal_Rela outrel;
4143 asection *sreloc;
4144 bfd_vma offset;
4145
4146 do_ifunc_pointer:
4147 /* Need a dynamic relocation to get the real function
4148 adddress. */
4149 offset = _bfd_elf_section_offset (output_bfd,
4150 info,
4151 input_section,
4152 rel->r_offset);
4153 if (offset == (bfd_vma) -1
4154 || offset == (bfd_vma) -2)
4155 abort ();
4156
4157 outrel.r_offset = (input_section->output_section->vma
4158 + input_section->output_offset
4159 + offset);
4160
4161 if (h->dynindx == -1
4162 || h->forced_local
4163 || bfd_link_executable (info))
4164 {
4165 /* This symbol is resolved locally. */
4166 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
4167 bfd_put_32 (output_bfd,
4168 (h->root.u.def.value
4169 + h->root.u.def.section->output_section->vma
4170 + h->root.u.def.section->output_offset),
4171 contents + offset);
4172 }
4173 else
4174 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4175
4176 /* Dynamic relocations are stored in
4177 1. .rel.ifunc section in PIC object.
4178 2. .rel.got section in dynamic executable.
4179 3. .rel.iplt section in static executable. */
4180 if (bfd_link_pic (info))
4181 sreloc = htab->elf.irelifunc;
4182 else if (htab->elf.splt != NULL)
4183 sreloc = htab->elf.srelgot;
4184 else
4185 sreloc = htab->elf.irelplt;
4186 elf_append_rel (output_bfd, sreloc, &outrel);
4187
4188 /* If this reloc is against an external symbol, we
4189 do not want to fiddle with the addend. Otherwise,
4190 we need to include the symbol value so that it
4191 becomes an addend for the dynamic reloc. For an
4192 internal symbol, we have updated addend. */
4193 continue;
4194 }
4195 /* FALLTHROUGH */
4196 case R_386_PC32:
4197 case R_386_PLT32:
4198 goto do_relocation;
4199
4200 case R_386_GOTOFF:
4201 relocation -= (gotplt->output_section->vma
4202 + gotplt->output_offset);
4203 goto do_relocation;
4204 }
4205 }
4206
4207 eh = (struct elf_i386_link_hash_entry *) h;
4208 resolved_to_zero = (eh != NULL
4209 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
4210 eh->has_got_reloc,
4211 eh));
4212
4213 switch (r_type)
4214 {
4215 case R_386_GOT32X:
4216 /* Avoid optimizing _DYNAMIC since ld.so may use its
4217 link-time address. */
4218 if (h == htab->elf.hdynamic)
4219 goto r_386_got32;
4220
4221 if (bfd_link_pic (info))
4222 {
4223 /* It is OK to convert mov to lea and convert indirect
4224 branch to direct branch. It is OK to convert adc,
4225 add, and, cmp, or, sbb, sub, test, xor only when PIC
4226 is false. */
4227 unsigned int opcode, addend;
4228 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4229 if (addend != 0)
4230 goto r_386_got32;
4231 opcode = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4232 if (opcode != 0x8b && opcode != 0xff)
4233 goto r_386_got32;
4234 }
4235
4236 /* Resolve "mov GOT[(%reg)], %reg",
4237 "call/jmp *GOT[(%reg)]", "test %reg, foo@GOT[(%reg)]"
4238 and "binop foo@GOT[(%reg)], %reg". */
4239 if (h == NULL
4240 || (h->plt.offset == (bfd_vma) -1
4241 && h->got.offset == (bfd_vma) -1)
4242 || htab->elf.sgotplt == NULL)
4243 abort ();
4244
4245 offplt = (htab->elf.sgotplt->output_section->vma
4246 + htab->elf.sgotplt->output_offset);
4247
4248 /* It is relative to .got.plt section. */
4249 if (h->got.offset != (bfd_vma) -1)
4250 /* Use GOT entry. Mask off the least significant bit in
4251 GOT offset which may be set by R_386_GOT32 processing
4252 below. */
4253 relocation = (htab->elf.sgot->output_section->vma
4254 + htab->elf.sgot->output_offset
4255 + (h->got.offset & ~1) - offplt);
4256 else
4257 /* Use GOTPLT entry. */
4258 relocation = (h->plt.offset / plt_entry_size - 1 + 3) * 4;
4259
4260 if (!bfd_link_pic (info))
4261 {
4262 /* If not PIC, add the .got.plt section address for
4263 baseless addressing. */
4264 unsigned int modrm;
4265 modrm = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4266 if ((modrm & 0xc7) == 0x5)
4267 relocation += offplt;
4268 }
4269
4270 unresolved_reloc = FALSE;
4271 break;
4272
4273 case R_386_GOT32:
4274 r_386_got32:
4275 /* Relocation is to the entry for this symbol in the global
4276 offset table. */
4277 if (htab->elf.sgot == NULL)
4278 abort ();
4279
4280 relative_reloc = FALSE;
4281 if (h != NULL)
4282 {
4283 bfd_boolean dyn;
4284
4285 off = h->got.offset;
4286 dyn = htab->elf.dynamic_sections_created;
4287 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4288 bfd_link_pic (info),
4289 h)
4290 || (bfd_link_pic (info)
4291 && SYMBOL_REFERENCES_LOCAL (info, h))
4292 || (ELF_ST_VISIBILITY (h->other)
4293 && h->root.type == bfd_link_hash_undefweak))
4294 {
4295 /* This is actually a static link, or it is a
4296 -Bsymbolic link and the symbol is defined
4297 locally, or the symbol was forced to be local
4298 because of a version file. We must initialize
4299 this entry in the global offset table. Since the
4300 offset must always be a multiple of 4, we use the
4301 least significant bit to record whether we have
4302 initialized it already.
4303
4304 When doing a dynamic link, we create a .rel.got
4305 relocation entry to initialize the value. This
4306 is done in the finish_dynamic_symbol routine. */
4307 if ((off & 1) != 0)
4308 off &= ~1;
4309 else
4310 {
4311 bfd_put_32 (output_bfd, relocation,
4312 htab->elf.sgot->contents + off);
4313 h->got.offset |= 1;
4314
4315 if (h->dynindx == -1
4316 && !h->forced_local
4317 && h->root.type != bfd_link_hash_undefweak
4318 && bfd_link_pie (info))
4319 {
4320 /* PR ld/21402: If this symbol isn't dynamic
4321 in PIE, generate R_386_RELATIVE here. */
4322 eh->no_finish_dynamic_symbol = 1;
4323 relative_reloc = TRUE;
4324 }
4325 }
4326 }
4327 else
4328 unresolved_reloc = FALSE;
4329 }
4330 else
4331 {
4332 if (local_got_offsets == NULL)
4333 abort ();
4334
4335 off = local_got_offsets[r_symndx];
4336
4337 /* The offset must always be a multiple of 4. We use
4338 the least significant bit to record whether we have
4339 already generated the necessary reloc. */
4340 if ((off & 1) != 0)
4341 off &= ~1;
4342 else
4343 {
4344 bfd_put_32 (output_bfd, relocation,
4345 htab->elf.sgot->contents + off);
4346 local_got_offsets[r_symndx] |= 1;
4347
4348 if (bfd_link_pic (info))
4349 relative_reloc = TRUE;
4350 }
4351 }
4352
4353 if (relative_reloc)
4354 {
4355 asection *s;
4356 Elf_Internal_Rela outrel;
4357
4358 s = htab->elf.srelgot;
4359 if (s == NULL)
4360 abort ();
4361
4362 outrel.r_offset = (htab->elf.sgot->output_section->vma
4363 + htab->elf.sgot->output_offset
4364 + off);
4365 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4366 elf_append_rel (output_bfd, s, &outrel);
4367 }
4368
4369 if (off >= (bfd_vma) -2)
4370 abort ();
4371
4372 relocation = (htab->elf.sgot->output_section->vma
4373 + htab->elf.sgot->output_offset + off);
4374 if (rel->r_offset > 1
4375 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
4376 && *(contents + rel->r_offset - 2) != 0x8d)
4377 {
4378 if (bfd_link_pic (info))
4379 {
4380 /* For PIC, disallow R_386_GOT32 without a base
4381 register, except for "lea foo@GOT, %reg", since
4382 we don't know what the GOT base is. */
4383 const char *name;
4384
4385 disallow_got32:
4386 if (h == NULL || h->root.root.string == NULL)
4387 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4388 NULL);
4389 else
4390 name = h->root.root.string;
4391
4392 _bfd_error_handler
4393 /* xgettext:c-format */
4394 (_("%B: direct GOT relocation %s against `%s'"
4395 " without base register can not be used"
4396 " when making a shared object"),
4397 input_bfd, howto->name, name);
4398 bfd_set_error (bfd_error_bad_value);
4399 return FALSE;
4400 }
4401 }
4402 else
4403 {
4404 /* Subtract the .got.plt section address only with a base
4405 register. */
4406 relocation -= (htab->elf.sgotplt->output_section->vma
4407 + htab->elf.sgotplt->output_offset);
4408 }
4409
4410 break;
4411
4412 case R_386_GOTOFF:
4413 /* Relocation is relative to the start of the global offset
4414 table. */
4415
4416 /* Check to make sure it isn't a protected function or data
4417 symbol for shared library since it may not be local when
4418 used as function address or with copy relocation. We also
4419 need to make sure that a symbol is referenced locally. */
4420 if (!bfd_link_executable (info) && h)
4421 {
4422 if (!h->def_regular)
4423 {
4424 const char *v;
4425
4426 switch (ELF_ST_VISIBILITY (h->other))
4427 {
4428 case STV_HIDDEN:
4429 v = _("hidden symbol");
4430 break;
4431 case STV_INTERNAL:
4432 v = _("internal symbol");
4433 break;
4434 case STV_PROTECTED:
4435 v = _("protected symbol");
4436 break;
4437 default:
4438 v = _("symbol");
4439 break;
4440 }
4441
4442 _bfd_error_handler
4443 /* xgettext:c-format */
4444 (_("%B: relocation R_386_GOTOFF against undefined %s"
4445 " `%s' can not be used when making a shared object"),
4446 input_bfd, v, h->root.root.string);
4447 bfd_set_error (bfd_error_bad_value);
4448 return FALSE;
4449 }
4450 else if (!SYMBOL_REFERENCES_LOCAL (info, h)
4451 && (h->type == STT_FUNC
4452 || h->type == STT_OBJECT)
4453 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
4454 {
4455 _bfd_error_handler
4456 /* xgettext:c-format */
4457 (_("%B: relocation R_386_GOTOFF against protected %s"
4458 " `%s' can not be used when making a shared object"),
4459 input_bfd,
4460 h->type == STT_FUNC ? "function" : "data",
4461 h->root.root.string);
4462 bfd_set_error (bfd_error_bad_value);
4463 return FALSE;
4464 }
4465 }
4466
4467 /* Note that sgot is not involved in this
4468 calculation. We always want the start of .got.plt. If we
4469 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
4470 permitted by the ABI, we might have to change this
4471 calculation. */
4472 relocation -= htab->elf.sgotplt->output_section->vma
4473 + htab->elf.sgotplt->output_offset;
4474 break;
4475
4476 case R_386_GOTPC:
4477 /* Use global offset table as symbol value. */
4478 relocation = htab->elf.sgotplt->output_section->vma
4479 + htab->elf.sgotplt->output_offset;
4480 unresolved_reloc = FALSE;
4481 break;
4482
4483 case R_386_PLT32:
4484 /* Relocation is to the entry for this symbol in the
4485 procedure linkage table. */
4486
4487 /* Resolve a PLT32 reloc against a local symbol directly,
4488 without using the procedure linkage table. */
4489 if (h == NULL)
4490 break;
4491
4492 if ((h->plt.offset == (bfd_vma) -1
4493 && eh->plt_got.offset == (bfd_vma) -1)
4494 || htab->elf.splt == NULL)
4495 {
4496 /* We didn't make a PLT entry for this symbol. This
4497 happens when statically linking PIC code, or when
4498 using -Bsymbolic. */
4499 break;
4500 }
4501
4502 if (h->plt.offset != (bfd_vma) -1)
4503 {
4504 resolved_plt = htab->elf.splt;
4505 plt_offset = h->plt.offset;
4506 }
4507 else
4508 {
4509 resolved_plt = htab->plt_got;
4510 plt_offset = eh->plt_got.offset;
4511 }
4512
4513 relocation = (resolved_plt->output_section->vma
4514 + resolved_plt->output_offset
4515 + plt_offset);
4516 unresolved_reloc = FALSE;
4517 break;
4518
4519 case R_386_SIZE32:
4520 /* Set to symbol size. */
4521 relocation = st_size;
4522 /* Fall through. */
4523
4524 case R_386_32:
4525 case R_386_PC32:
4526 if ((input_section->flags & SEC_ALLOC) == 0
4527 || is_vxworks_tls)
4528 break;
4529
4530 /* Copy dynamic function pointer relocations. Don't generate
4531 dynamic relocations against resolved undefined weak symbols
4532 in PIE, except for R_386_PC32. */
4533 if ((bfd_link_pic (info)
4534 && (h == NULL
4535 || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4536 && (!resolved_to_zero
4537 || r_type == R_386_PC32))
4538 || h->root.type != bfd_link_hash_undefweak))
4539 && ((r_type != R_386_PC32 && r_type != R_386_SIZE32)
4540 || !SYMBOL_CALLS_LOCAL (info, h)))
4541 || (ELIMINATE_COPY_RELOCS
4542 && !bfd_link_pic (info)
4543 && h != NULL
4544 && h->dynindx != -1
4545 && (!h->non_got_ref
4546 || eh->func_pointer_refcount > 0
4547 || (h->root.type == bfd_link_hash_undefweak
4548 && !resolved_to_zero))
4549 && ((h->def_dynamic && !h->def_regular)
4550 /* Undefined weak symbol is bound locally when
4551 PIC is false. */
4552 || h->root.type == bfd_link_hash_undefweak)))
4553 {
4554 Elf_Internal_Rela outrel;
4555 bfd_boolean skip, relocate;
4556 asection *sreloc;
4557
4558 /* When generating a shared object, these relocations
4559 are copied into the output file to be resolved at run
4560 time. */
4561
4562 skip = FALSE;
4563 relocate = FALSE;
4564
4565 outrel.r_offset =
4566 _bfd_elf_section_offset (output_bfd, info, input_section,
4567 rel->r_offset);
4568 if (outrel.r_offset == (bfd_vma) -1)
4569 skip = TRUE;
4570 else if (outrel.r_offset == (bfd_vma) -2)
4571 skip = TRUE, relocate = TRUE;
4572 outrel.r_offset += (input_section->output_section->vma
4573 + input_section->output_offset);
4574
4575 if (skip)
4576 memset (&outrel, 0, sizeof outrel);
4577 else if (h != NULL
4578 && h->dynindx != -1
4579 && (r_type == R_386_PC32
4580 || !(bfd_link_executable (info)
4581 || SYMBOLIC_BIND (info, h))
4582 || !h->def_regular))
4583 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4584 else
4585 {
4586 /* This symbol is local, or marked to become local. */
4587 relocate = TRUE;
4588 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4589 }
4590
4591 sreloc = elf_section_data (input_section)->sreloc;
4592
4593 if (sreloc == NULL || sreloc->contents == NULL)
4594 {
4595 r = bfd_reloc_notsupported;
4596 goto check_relocation_error;
4597 }
4598
4599 elf_append_rel (output_bfd, sreloc, &outrel);
4600
4601 /* If this reloc is against an external symbol, we do
4602 not want to fiddle with the addend. Otherwise, we
4603 need to include the symbol value so that it becomes
4604 an addend for the dynamic reloc. */
4605 if (! relocate)
4606 continue;
4607 }
4608 break;
4609
4610 case R_386_TLS_IE:
4611 if (!bfd_link_executable (info))
4612 {
4613 Elf_Internal_Rela outrel;
4614 asection *sreloc;
4615
4616 outrel.r_offset = rel->r_offset
4617 + input_section->output_section->vma
4618 + input_section->output_offset;
4619 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4620 sreloc = elf_section_data (input_section)->sreloc;
4621 if (sreloc == NULL)
4622 abort ();
4623 elf_append_rel (output_bfd, sreloc, &outrel);
4624 }
4625 /* Fall through */
4626
4627 case R_386_TLS_GD:
4628 case R_386_TLS_GOTDESC:
4629 case R_386_TLS_DESC_CALL:
4630 case R_386_TLS_IE_32:
4631 case R_386_TLS_GOTIE:
4632 tls_type = GOT_UNKNOWN;
4633 if (h == NULL && local_got_offsets)
4634 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
4635 else if (h != NULL)
4636 tls_type = elf_i386_hash_entry(h)->tls_type;
4637 if (tls_type == GOT_TLS_IE)
4638 tls_type = GOT_TLS_IE_NEG;
4639
4640 if (! elf_i386_tls_transition (info, input_bfd,
4641 input_section, contents,
4642 symtab_hdr, sym_hashes,
4643 &r_type, tls_type, rel,
4644 relend, h, r_symndx, TRUE))
4645 return FALSE;
4646
4647 if (r_type == R_386_TLS_LE_32)
4648 {
4649 BFD_ASSERT (! unresolved_reloc);
4650 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
4651 {
4652 unsigned int type;
4653 bfd_vma roff;
4654
4655 /* GD->LE transition. */
4656 type = *(contents + rel->r_offset - 2);
4657 if (type == 0x04)
4658 {
4659 /* Change
4660 leal foo@tlsgd(,%ebx,1), %eax
4661 call ___tls_get_addr@PLT
4662 into:
4663 movl %gs:0, %eax
4664 subl $foo@tpoff, %eax
4665 (6 byte form of subl). */
4666 roff = rel->r_offset + 5;
4667 }
4668 else
4669 {
4670 /* Change
4671 leal foo@tlsgd(%ebx), %eax
4672 call ___tls_get_addr@PLT
4673 nop
4674 or
4675 leal foo@tlsgd(%reg), %eax
4676 call *___tls_get_addr@GOT(%reg)
4677 which may be converted to
4678 addr32 call ___tls_get_addr
4679 into:
4680 movl %gs:0, %eax; subl $foo@tpoff, %eax
4681 (6 byte form of subl). */
4682 roff = rel->r_offset + 6;
4683 }
4684 memcpy (contents + roff - 8,
4685 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
4686 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
4687 contents + roff);
4688 /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */
4689 rel++;
4690 wrel++;
4691 continue;
4692 }
4693 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
4694 {
4695 /* GDesc -> LE transition.
4696 It's originally something like:
4697 leal x@tlsdesc(%ebx), %eax
4698
4699 leal x@ntpoff, %eax
4700
4701 Registers other than %eax may be set up here. */
4702
4703 unsigned int val;
4704 bfd_vma roff;
4705
4706 roff = rel->r_offset;
4707 val = bfd_get_8 (input_bfd, contents + roff - 1);
4708
4709 /* Now modify the instruction as appropriate. */
4710 /* aoliva FIXME: remove the above and xor the byte
4711 below with 0x86. */
4712 bfd_put_8 (output_bfd, val ^ 0x86,
4713 contents + roff - 1);
4714 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4715 contents + roff);
4716 continue;
4717 }
4718 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
4719 {
4720 /* GDesc -> LE transition.
4721 It's originally:
4722 call *(%eax)
4723 Turn it into:
4724 xchg %ax,%ax */
4725
4726 bfd_vma roff;
4727
4728 roff = rel->r_offset;
4729 bfd_put_8 (output_bfd, 0x66, contents + roff);
4730 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4731 continue;
4732 }
4733 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
4734 {
4735 unsigned int val;
4736
4737 /* IE->LE transition:
4738 Originally it can be one of:
4739 movl foo, %eax
4740 movl foo, %reg
4741 addl foo, %reg
4742 We change it into:
4743 movl $foo, %eax
4744 movl $foo, %reg
4745 addl $foo, %reg. */
4746 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4747 if (val == 0xa1)
4748 {
4749 /* movl foo, %eax. */
4750 bfd_put_8 (output_bfd, 0xb8,
4751 contents + rel->r_offset - 1);
4752 }
4753 else
4754 {
4755 unsigned int type;
4756
4757 type = bfd_get_8 (input_bfd,
4758 contents + rel->r_offset - 2);
4759 switch (type)
4760 {
4761 case 0x8b:
4762 /* movl */
4763 bfd_put_8 (output_bfd, 0xc7,
4764 contents + rel->r_offset - 2);
4765 bfd_put_8 (output_bfd,
4766 0xc0 | ((val >> 3) & 7),
4767 contents + rel->r_offset - 1);
4768 break;
4769 case 0x03:
4770 /* addl */
4771 bfd_put_8 (output_bfd, 0x81,
4772 contents + rel->r_offset - 2);
4773 bfd_put_8 (output_bfd,
4774 0xc0 | ((val >> 3) & 7),
4775 contents + rel->r_offset - 1);
4776 break;
4777 default:
4778 BFD_FAIL ();
4779 break;
4780 }
4781 }
4782 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4783 contents + rel->r_offset);
4784 continue;
4785 }
4786 else
4787 {
4788 unsigned int val, type;
4789
4790 /* {IE_32,GOTIE}->LE transition:
4791 Originally it can be one of:
4792 subl foo(%reg1), %reg2
4793 movl foo(%reg1), %reg2
4794 addl foo(%reg1), %reg2
4795 We change it into:
4796 subl $foo, %reg2
4797 movl $foo, %reg2 (6 byte form)
4798 addl $foo, %reg2. */
4799 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4800 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4801 if (type == 0x8b)
4802 {
4803 /* movl */
4804 bfd_put_8 (output_bfd, 0xc7,
4805 contents + rel->r_offset - 2);
4806 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4807 contents + rel->r_offset - 1);
4808 }
4809 else if (type == 0x2b)
4810 {
4811 /* subl */
4812 bfd_put_8 (output_bfd, 0x81,
4813 contents + rel->r_offset - 2);
4814 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
4815 contents + rel->r_offset - 1);
4816 }
4817 else if (type == 0x03)
4818 {
4819 /* addl */
4820 bfd_put_8 (output_bfd, 0x81,
4821 contents + rel->r_offset - 2);
4822 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4823 contents + rel->r_offset - 1);
4824 }
4825 else
4826 BFD_FAIL ();
4827 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
4828 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4829 contents + rel->r_offset);
4830 else
4831 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
4832 contents + rel->r_offset);
4833 continue;
4834 }
4835 }
4836
4837 if (htab->elf.sgot == NULL)
4838 abort ();
4839
4840 if (h != NULL)
4841 {
4842 off = h->got.offset;
4843 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
4844 }
4845 else
4846 {
4847 if (local_got_offsets == NULL)
4848 abort ();
4849
4850 off = local_got_offsets[r_symndx];
4851 offplt = local_tlsdesc_gotents[r_symndx];
4852 }
4853
4854 if ((off & 1) != 0)
4855 off &= ~1;
4856 else
4857 {
4858 Elf_Internal_Rela outrel;
4859 int dr_type;
4860 asection *sreloc;
4861
4862 if (htab->elf.srelgot == NULL)
4863 abort ();
4864
4865 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4866
4867 if (GOT_TLS_GDESC_P (tls_type))
4868 {
4869 bfd_byte *loc;
4870 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
4871 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
4872 <= htab->elf.sgotplt->size);
4873 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4874 + htab->elf.sgotplt->output_offset
4875 + offplt
4876 + htab->sgotplt_jump_table_size);
4877 sreloc = htab->elf.srelplt;
4878 loc = sreloc->contents;
4879 loc += (htab->next_tls_desc_index++
4880 * sizeof (Elf32_External_Rel));
4881 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
4882 <= sreloc->contents + sreloc->size);
4883 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
4884 if (indx == 0)
4885 {
4886 BFD_ASSERT (! unresolved_reloc);
4887 bfd_put_32 (output_bfd,
4888 relocation - elf_i386_dtpoff_base (info),
4889 htab->elf.sgotplt->contents + offplt
4890 + htab->sgotplt_jump_table_size + 4);
4891 }
4892 else
4893 {
4894 bfd_put_32 (output_bfd, 0,
4895 htab->elf.sgotplt->contents + offplt
4896 + htab->sgotplt_jump_table_size + 4);
4897 }
4898 }
4899
4900 sreloc = htab->elf.srelgot;
4901
4902 outrel.r_offset = (htab->elf.sgot->output_section->vma
4903 + htab->elf.sgot->output_offset + off);
4904
4905 if (GOT_TLS_GD_P (tls_type))
4906 dr_type = R_386_TLS_DTPMOD32;
4907 else if (GOT_TLS_GDESC_P (tls_type))
4908 goto dr_done;
4909 else if (tls_type == GOT_TLS_IE_POS)
4910 dr_type = R_386_TLS_TPOFF;
4911 else
4912 dr_type = R_386_TLS_TPOFF32;
4913
4914 if (dr_type == R_386_TLS_TPOFF && indx == 0)
4915 bfd_put_32 (output_bfd,
4916 relocation - elf_i386_dtpoff_base (info),
4917 htab->elf.sgot->contents + off);
4918 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
4919 bfd_put_32 (output_bfd,
4920 elf_i386_dtpoff_base (info) - relocation,
4921 htab->elf.sgot->contents + off);
4922 else if (dr_type != R_386_TLS_DESC)
4923 bfd_put_32 (output_bfd, 0,
4924 htab->elf.sgot->contents + off);
4925 outrel.r_info = ELF32_R_INFO (indx, dr_type);
4926
4927 elf_append_rel (output_bfd, sreloc, &outrel);
4928
4929 if (GOT_TLS_GD_P (tls_type))
4930 {
4931 if (indx == 0)
4932 {
4933 BFD_ASSERT (! unresolved_reloc);
4934 bfd_put_32 (output_bfd,
4935 relocation - elf_i386_dtpoff_base (info),
4936 htab->elf.sgot->contents + off + 4);
4937 }
4938 else
4939 {
4940 bfd_put_32 (output_bfd, 0,
4941 htab->elf.sgot->contents + off + 4);
4942 outrel.r_info = ELF32_R_INFO (indx,
4943 R_386_TLS_DTPOFF32);
4944 outrel.r_offset += 4;
4945 elf_append_rel (output_bfd, sreloc, &outrel);
4946 }
4947 }
4948 else if (tls_type == GOT_TLS_IE_BOTH)
4949 {
4950 bfd_put_32 (output_bfd,
4951 (indx == 0
4952 ? relocation - elf_i386_dtpoff_base (info)
4953 : 0),
4954 htab->elf.sgot->contents + off + 4);
4955 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
4956 outrel.r_offset += 4;
4957 elf_append_rel (output_bfd, sreloc, &outrel);
4958 }
4959
4960 dr_done:
4961 if (h != NULL)
4962 h->got.offset |= 1;
4963 else
4964 local_got_offsets[r_symndx] |= 1;
4965 }
4966
4967 if (off >= (bfd_vma) -2
4968 && ! GOT_TLS_GDESC_P (tls_type))
4969 abort ();
4970 if (r_type == R_386_TLS_GOTDESC
4971 || r_type == R_386_TLS_DESC_CALL)
4972 {
4973 relocation = htab->sgotplt_jump_table_size + offplt;
4974 unresolved_reloc = FALSE;
4975 }
4976 else if (r_type == ELF32_R_TYPE (rel->r_info))
4977 {
4978 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
4979 + htab->elf.sgotplt->output_offset;
4980 relocation = htab->elf.sgot->output_section->vma
4981 + htab->elf.sgot->output_offset + off - g_o_t;
4982 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
4983 && tls_type == GOT_TLS_IE_BOTH)
4984 relocation += 4;
4985 if (r_type == R_386_TLS_IE)
4986 relocation += g_o_t;
4987 unresolved_reloc = FALSE;
4988 }
4989 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
4990 {
4991 unsigned int val, type;
4992 bfd_vma roff;
4993
4994 /* GD->IE transition. */
4995 type = *(contents + rel->r_offset - 2);
4996 val = *(contents + rel->r_offset - 1);
4997 if (type == 0x04)
4998 {
4999 /* Change
5000 leal foo@tlsgd(,%ebx,1), %eax
5001 call ___tls_get_addr@PLT
5002 into:
5003 movl %gs:0, %eax
5004 subl $foo@gottpoff(%ebx), %eax. */
5005 val >>= 3;
5006 roff = rel->r_offset - 3;
5007 }
5008 else
5009 {
5010 /* Change
5011 leal foo@tlsgd(%ebx), %eax
5012 call ___tls_get_addr@PLT
5013 nop
5014 or
5015 leal foo@tlsgd(%reg), %eax
5016 call *___tls_get_addr@GOT(%reg)
5017 which may be converted to
5018 addr32 call ___tls_get_addr
5019 into:
5020 movl %gs:0, %eax;
5021 subl $foo@gottpoff(%reg), %eax. */
5022 roff = rel->r_offset - 2;
5023 }
5024 memcpy (contents + roff,
5025 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
5026 contents[roff + 7] = 0x80 | (val & 7);
5027 /* If foo is used only with foo@gotntpoff(%reg) and
5028 foo@indntpoff, but not with foo@gottpoff(%reg), change
5029 subl $foo@gottpoff(%reg), %eax
5030 into:
5031 addl $foo@gotntpoff(%reg), %eax. */
5032 if (tls_type == GOT_TLS_IE_POS)
5033 contents[roff + 6] = 0x03;
5034 bfd_put_32 (output_bfd,
5035 htab->elf.sgot->output_section->vma
5036 + htab->elf.sgot->output_offset + off
5037 - htab->elf.sgotplt->output_section->vma
5038 - htab->elf.sgotplt->output_offset,
5039 contents + roff + 8);
5040 /* Skip R_386_PLT32 and R_386_GOT32X. */
5041 rel++;
5042 wrel++;
5043 continue;
5044 }
5045 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
5046 {
5047 /* GDesc -> IE transition.
5048 It's originally something like:
5049 leal x@tlsdesc(%ebx), %eax
5050
5051 Change it to:
5052 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
5053 or:
5054 movl x@gottpoff(%ebx), %eax # before negl %eax
5055
5056 Registers other than %eax may be set up here. */
5057
5058 bfd_vma roff;
5059
5060 /* First, make sure it's a leal adding ebx to a 32-bit
5061 offset into any register, although it's probably
5062 almost always going to be eax. */
5063 roff = rel->r_offset;
5064
5065 /* Now modify the instruction as appropriate. */
5066 /* To turn a leal into a movl in the form we use it, it
5067 suffices to change the first byte from 0x8d to 0x8b.
5068 aoliva FIXME: should we decide to keep the leal, all
5069 we have to do is remove the statement below, and
5070 adjust the relaxation of R_386_TLS_DESC_CALL. */
5071 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
5072
5073 if (tls_type == GOT_TLS_IE_BOTH)
5074 off += 4;
5075
5076 bfd_put_32 (output_bfd,
5077 htab->elf.sgot->output_section->vma
5078 + htab->elf.sgot->output_offset + off
5079 - htab->elf.sgotplt->output_section->vma
5080 - htab->elf.sgotplt->output_offset,
5081 contents + roff);
5082 continue;
5083 }
5084 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
5085 {
5086 /* GDesc -> IE transition.
5087 It's originally:
5088 call *(%eax)
5089
5090 Change it to:
5091 xchg %ax,%ax
5092 or
5093 negl %eax
5094 depending on how we transformed the TLS_GOTDESC above.
5095 */
5096
5097 bfd_vma roff;
5098
5099 roff = rel->r_offset;
5100
5101 /* Now modify the instruction as appropriate. */
5102 if (tls_type != GOT_TLS_IE_NEG)
5103 {
5104 /* xchg %ax,%ax */
5105 bfd_put_8 (output_bfd, 0x66, contents + roff);
5106 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5107 }
5108 else
5109 {
5110 /* negl %eax */
5111 bfd_put_8 (output_bfd, 0xf7, contents + roff);
5112 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
5113 }
5114
5115 continue;
5116 }
5117 else
5118 BFD_ASSERT (FALSE);
5119 break;
5120
5121 case R_386_TLS_LDM:
5122 if (! elf_i386_tls_transition (info, input_bfd,
5123 input_section, contents,
5124 symtab_hdr, sym_hashes,
5125 &r_type, GOT_UNKNOWN, rel,
5126 relend, h, r_symndx, TRUE))
5127 return FALSE;
5128
5129 if (r_type != R_386_TLS_LDM)
5130 {
5131 /* LD->LE transition. Change
5132 leal foo@tlsldm(%ebx) %eax
5133 call ___tls_get_addr@PLT
5134 into:
5135 movl %gs:0, %eax
5136 nop
5137 leal 0(%esi,1), %esi
5138 or change
5139 leal foo@tlsldm(%reg) %eax
5140 call *___tls_get_addr@GOT(%reg)
5141 which may be converted to
5142 addr32 call ___tls_get_addr
5143 into:
5144 movl %gs:0, %eax
5145 leal 0(%esi), %esi */
5146 BFD_ASSERT (r_type == R_386_TLS_LE_32);
5147 if (*(contents + rel->r_offset + 4) == 0xff
5148 || *(contents + rel->r_offset + 4) == 0x67)
5149 memcpy (contents + rel->r_offset - 2,
5150 "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
5151 else
5152 memcpy (contents + rel->r_offset - 2,
5153 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
5154 /* Skip R_386_PC32/R_386_PLT32. */
5155 rel++;
5156 wrel++;
5157 continue;
5158 }
5159
5160 if (htab->elf.sgot == NULL)
5161 abort ();
5162
5163 off = htab->tls_ldm_got.offset;
5164 if (off & 1)
5165 off &= ~1;
5166 else
5167 {
5168 Elf_Internal_Rela outrel;
5169
5170 if (htab->elf.srelgot == NULL)
5171 abort ();
5172
5173 outrel.r_offset = (htab->elf.sgot->output_section->vma
5174 + htab->elf.sgot->output_offset + off);
5175
5176 bfd_put_32 (output_bfd, 0,
5177 htab->elf.sgot->contents + off);
5178 bfd_put_32 (output_bfd, 0,
5179 htab->elf.sgot->contents + off + 4);
5180 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
5181 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
5182 htab->tls_ldm_got.offset |= 1;
5183 }
5184 relocation = htab->elf.sgot->output_section->vma
5185 + htab->elf.sgot->output_offset + off
5186 - htab->elf.sgotplt->output_section->vma
5187 - htab->elf.sgotplt->output_offset;
5188 unresolved_reloc = FALSE;
5189 break;
5190
5191 case R_386_TLS_LDO_32:
5192 if (!bfd_link_executable (info)
5193 || (input_section->flags & SEC_CODE) == 0)
5194 relocation -= elf_i386_dtpoff_base (info);
5195 else
5196 /* When converting LDO to LE, we must negate. */
5197 relocation = -elf_i386_tpoff (info, relocation);
5198 break;
5199
5200 case R_386_TLS_LE_32:
5201 case R_386_TLS_LE:
5202 if (!bfd_link_executable (info))
5203 {
5204 Elf_Internal_Rela outrel;
5205 asection *sreloc;
5206
5207 outrel.r_offset = rel->r_offset
5208 + input_section->output_section->vma
5209 + input_section->output_offset;
5210 if (h != NULL && h->dynindx != -1)
5211 indx = h->dynindx;
5212 else
5213 indx = 0;
5214 if (r_type == R_386_TLS_LE_32)
5215 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
5216 else
5217 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
5218 sreloc = elf_section_data (input_section)->sreloc;
5219 if (sreloc == NULL)
5220 abort ();
5221 elf_append_rel (output_bfd, sreloc, &outrel);
5222 if (indx)
5223 continue;
5224 else if (r_type == R_386_TLS_LE_32)
5225 relocation = elf_i386_dtpoff_base (info) - relocation;
5226 else
5227 relocation -= elf_i386_dtpoff_base (info);
5228 }
5229 else if (r_type == R_386_TLS_LE_32)
5230 relocation = elf_i386_tpoff (info, relocation);
5231 else
5232 relocation = -elf_i386_tpoff (info, relocation);
5233 break;
5234
5235 default:
5236 break;
5237 }
5238
5239 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5240 because such sections are not SEC_ALLOC and thus ld.so will
5241 not process them. */
5242 if (unresolved_reloc
5243 && !((input_section->flags & SEC_DEBUGGING) != 0
5244 && h->def_dynamic)
5245 && _bfd_elf_section_offset (output_bfd, info, input_section,
5246 rel->r_offset) != (bfd_vma) -1)
5247 {
5248 _bfd_error_handler
5249 /* xgettext:c-format */
5250 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5251 input_bfd,
5252 input_section,
5253 (long) rel->r_offset,
5254 howto->name,
5255 h->root.root.string);
5256 return FALSE;
5257 }
5258
5259 do_relocation:
5260 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5261 contents, rel->r_offset,
5262 relocation, 0);
5263
5264 check_relocation_error:
5265 if (r != bfd_reloc_ok)
5266 {
5267 const char *name;
5268
5269 if (h != NULL)
5270 name = h->root.root.string;
5271 else
5272 {
5273 name = bfd_elf_string_from_elf_section (input_bfd,
5274 symtab_hdr->sh_link,
5275 sym->st_name);
5276 if (name == NULL)
5277 return FALSE;
5278 if (*name == '\0')
5279 name = bfd_section_name (input_bfd, sec);
5280 }
5281
5282 if (r == bfd_reloc_overflow)
5283 (*info->callbacks->reloc_overflow)
5284 (info, (h ? &h->root : NULL), name, howto->name,
5285 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5286 else
5287 {
5288 _bfd_error_handler
5289 /* xgettext:c-format */
5290 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
5291 input_bfd, input_section,
5292 (long) rel->r_offset, name, (int) r);
5293 return FALSE;
5294 }
5295 }
5296
5297 if (wrel != rel)
5298 *wrel = *rel;
5299 }
5300
5301 if (wrel != rel)
5302 {
5303 Elf_Internal_Shdr *rel_hdr;
5304 size_t deleted = rel - wrel;
5305
5306 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
5307 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5308 if (rel_hdr->sh_size == 0)
5309 {
5310 /* It is too late to remove an empty reloc section. Leave
5311 one NONE reloc.
5312 ??? What is wrong with an empty section??? */
5313 rel_hdr->sh_size = rel_hdr->sh_entsize;
5314 deleted -= 1;
5315 }
5316 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5317 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5318 input_section->reloc_count -= deleted;
5319 }
5320
5321 return TRUE;
5322 }
5323
5324 /* Finish up dynamic symbol handling. We set the contents of various
5325 dynamic sections here. */
5326
5327 static bfd_boolean
5328 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
5329 struct bfd_link_info *info,
5330 struct elf_link_hash_entry *h,
5331 Elf_Internal_Sym *sym)
5332 {
5333 struct elf_i386_link_hash_table *htab;
5334 unsigned plt_entry_size;
5335 const struct elf_i386_backend_data *abed;
5336 struct elf_i386_link_hash_entry *eh;
5337 bfd_boolean local_undefweak;
5338
5339 htab = elf_i386_hash_table (info);
5340 if (htab == NULL)
5341 return FALSE;
5342
5343 abed = get_elf_i386_backend_data (output_bfd);
5344 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
5345
5346 eh = (struct elf_i386_link_hash_entry *) h;
5347 if (eh->no_finish_dynamic_symbol)
5348 abort ();
5349
5350 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5351 resolved undefined weak symbols in executable so that their
5352 references have value 0 at run-time. */
5353 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
5354 eh->has_got_reloc,
5355 eh);
5356
5357 if (h->plt.offset != (bfd_vma) -1)
5358 {
5359 bfd_vma plt_index;
5360 bfd_vma got_offset;
5361 Elf_Internal_Rela rel;
5362 bfd_byte *loc;
5363 asection *plt, *gotplt, *relplt;
5364
5365 /* When building a static executable, use .iplt, .igot.plt and
5366 .rel.iplt sections for STT_GNU_IFUNC symbols. */
5367 if (htab->elf.splt != NULL)
5368 {
5369 plt = htab->elf.splt;
5370 gotplt = htab->elf.sgotplt;
5371 relplt = htab->elf.srelplt;
5372 }
5373 else
5374 {
5375 plt = htab->elf.iplt;
5376 gotplt = htab->elf.igotplt;
5377 relplt = htab->elf.irelplt;
5378 }
5379
5380 /* This symbol has an entry in the procedure linkage table. Set
5381 it up. */
5382
5383 if ((h->dynindx == -1
5384 && !local_undefweak
5385 && !((h->forced_local || bfd_link_executable (info))
5386 && h->def_regular
5387 && h->type == STT_GNU_IFUNC))
5388 || plt == NULL
5389 || gotplt == NULL
5390 || relplt == NULL)
5391 abort ();
5392
5393 /* Get the index in the procedure linkage table which
5394 corresponds to this symbol. This is the index of this symbol
5395 in all the symbols for which we are making plt entries. The
5396 first entry in the procedure linkage table is reserved.
5397
5398 Get the offset into the .got table of the entry that
5399 corresponds to this function. Each .got entry is 4 bytes.
5400 The first three are reserved.
5401
5402 For static executables, we don't reserve anything. */
5403
5404 if (plt == htab->elf.splt)
5405 {
5406 got_offset = h->plt.offset / plt_entry_size - 1;
5407 got_offset = (got_offset + 3) * 4;
5408 }
5409 else
5410 {
5411 got_offset = h->plt.offset / plt_entry_size;
5412 got_offset = got_offset * 4;
5413 }
5414
5415 /* Fill in the entry in the procedure linkage table. */
5416 if (! bfd_link_pic (info))
5417 {
5418 memcpy (plt->contents + h->plt.offset, abed->plt->plt_entry,
5419 abed->plt->plt_entry_size);
5420 bfd_put_32 (output_bfd,
5421 (gotplt->output_section->vma
5422 + gotplt->output_offset
5423 + got_offset),
5424 plt->contents + h->plt.offset
5425 + abed->plt->plt_got_offset);
5426
5427 if (abed->is_vxworks)
5428 {
5429 int s, k, reloc_index;
5430
5431 /* Create the R_386_32 relocation referencing the GOT
5432 for this PLT entry. */
5433
5434 /* S: Current slot number (zero-based). */
5435 s = ((h->plt.offset - abed->plt->plt_entry_size)
5436 / abed->plt->plt_entry_size);
5437 /* K: Number of relocations for PLTResolve. */
5438 if (bfd_link_pic (info))
5439 k = PLTRESOLVE_RELOCS_SHLIB;
5440 else
5441 k = PLTRESOLVE_RELOCS;
5442 /* Skip the PLTresolve relocations, and the relocations for
5443 the other PLT slots. */
5444 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
5445 loc = (htab->srelplt2->contents + reloc_index
5446 * sizeof (Elf32_External_Rel));
5447
5448 rel.r_offset = (htab->elf.splt->output_section->vma
5449 + htab->elf.splt->output_offset
5450 + h->plt.offset + 2),
5451 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5452 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5453
5454 /* Create the R_386_32 relocation referencing the beginning of
5455 the PLT for this GOT entry. */
5456 rel.r_offset = (htab->elf.sgotplt->output_section->vma
5457 + htab->elf.sgotplt->output_offset
5458 + got_offset);
5459 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
5460 bfd_elf32_swap_reloc_out (output_bfd, &rel,
5461 loc + sizeof (Elf32_External_Rel));
5462 }
5463 }
5464 else
5465 {
5466 memcpy (plt->contents + h->plt.offset, abed->plt->pic_plt_entry,
5467 abed->plt->plt_entry_size);
5468 bfd_put_32 (output_bfd, got_offset,
5469 plt->contents + h->plt.offset
5470 + abed->plt->plt_got_offset);
5471 }
5472
5473 /* Fill in the entry in the global offset table. Leave the entry
5474 as zero for undefined weak symbol in PIE. No PLT relocation
5475 against undefined weak symbol in PIE. */
5476 if (!local_undefweak)
5477 {
5478 bfd_put_32 (output_bfd,
5479 (plt->output_section->vma
5480 + plt->output_offset
5481 + h->plt.offset
5482 + abed->plt->plt_lazy_offset),
5483 gotplt->contents + got_offset);
5484
5485 /* Fill in the entry in the .rel.plt section. */
5486 rel.r_offset = (gotplt->output_section->vma
5487 + gotplt->output_offset
5488 + got_offset);
5489 if (h->dynindx == -1
5490 || ((bfd_link_executable (info)
5491 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5492 && h->def_regular
5493 && h->type == STT_GNU_IFUNC))
5494 {
5495 /* If an STT_GNU_IFUNC symbol is locally defined, generate
5496 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
5497 in the .got.plt section. */
5498 bfd_put_32 (output_bfd,
5499 (h->root.u.def.value
5500 + h->root.u.def.section->output_section->vma
5501 + h->root.u.def.section->output_offset),
5502 gotplt->contents + got_offset);
5503 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
5504 /* R_386_IRELATIVE comes last. */
5505 plt_index = htab->next_irelative_index--;
5506 }
5507 else
5508 {
5509 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
5510 plt_index = htab->next_jump_slot_index++;
5511 }
5512
5513 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
5514 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5515
5516 /* Don't fill PLT entry for static executables. */
5517 if (plt == htab->elf.splt)
5518 {
5519 bfd_put_32 (output_bfd,
5520 plt_index * sizeof (Elf32_External_Rel),
5521 plt->contents + h->plt.offset
5522 + abed->plt->plt_reloc_offset);
5523 bfd_put_32 (output_bfd, - (h->plt.offset
5524 + abed->plt->plt_plt_offset + 4),
5525 plt->contents + h->plt.offset
5526 + abed->plt->plt_plt_offset);
5527 }
5528 }
5529 }
5530 else if (eh->plt_got.offset != (bfd_vma) -1)
5531 {
5532 bfd_vma got_offset, plt_offset;
5533 asection *plt, *got, *gotplt;
5534 const bfd_byte *got_plt_entry;
5535
5536 /* Offset of displacement of the indirect jump. */
5537 bfd_vma plt_got_offset = 2;
5538
5539 /* Set the entry in the GOT procedure linkage table. */
5540 plt = htab->plt_got;
5541 got = htab->elf.sgot;
5542 gotplt = htab->elf.sgotplt;
5543 got_offset = h->got.offset;
5544
5545 if (got_offset == (bfd_vma) -1
5546 || plt == NULL
5547 || got == NULL
5548 || gotplt == NULL)
5549 abort ();
5550
5551 /* Fill in the entry in the GOT procedure linkage table. */
5552 if (! bfd_link_pic (info))
5553 {
5554 got_plt_entry = elf_i386_got_plt_entry;
5555 got_offset += got->output_section->vma + got->output_offset;
5556 }
5557 else
5558 {
5559 got_plt_entry = elf_i386_pic_got_plt_entry;
5560 got_offset += (got->output_section->vma
5561 + got->output_offset
5562 - gotplt->output_section->vma
5563 - gotplt->output_offset);
5564 }
5565
5566 plt_offset = eh->plt_got.offset;
5567 memcpy (plt->contents + plt_offset, got_plt_entry,
5568 sizeof (elf_i386_got_plt_entry));
5569 bfd_put_32 (output_bfd, got_offset,
5570 plt->contents + plt_offset + plt_got_offset);
5571 }
5572
5573 if (!local_undefweak
5574 && !h->def_regular
5575 && (h->plt.offset != (bfd_vma) -1
5576 || eh->plt_got.offset != (bfd_vma) -1))
5577 {
5578 /* Mark the symbol as undefined, rather than as defined in
5579 the .plt section. Leave the value if there were any
5580 relocations where pointer equality matters (this is a clue
5581 for the dynamic linker, to make function pointer
5582 comparisons work between an application and shared
5583 library), otherwise set it to zero. If a function is only
5584 called from a binary, there is no need to slow down
5585 shared libraries because of that. */
5586 sym->st_shndx = SHN_UNDEF;
5587 if (!h->pointer_equality_needed)
5588 sym->st_value = 0;
5589 }
5590
5591 /* Don't generate dynamic GOT relocation against undefined weak
5592 symbol in executable. */
5593 if (h->got.offset != (bfd_vma) -1
5594 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
5595 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
5596 && !local_undefweak)
5597 {
5598 Elf_Internal_Rela rel;
5599 asection *relgot = htab->elf.srelgot;
5600
5601 /* This symbol has an entry in the global offset table. Set it
5602 up. */
5603
5604 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
5605 abort ();
5606
5607 rel.r_offset = (htab->elf.sgot->output_section->vma
5608 + htab->elf.sgot->output_offset
5609 + (h->got.offset & ~(bfd_vma) 1));
5610
5611 /* If this is a static link, or it is a -Bsymbolic link and the
5612 symbol is defined locally or was forced to be local because
5613 of a version file, we just want to emit a RELATIVE reloc.
5614 The entry in the global offset table will already have been
5615 initialized in the relocate_section function. */
5616 if (h->def_regular
5617 && h->type == STT_GNU_IFUNC)
5618 {
5619 if (h->plt.offset == (bfd_vma) -1)
5620 {
5621 /* STT_GNU_IFUNC is referenced without PLT. */
5622 if (htab->elf.splt == NULL)
5623 {
5624 /* use .rel[a].iplt section to store .got relocations
5625 in static executable. */
5626 relgot = htab->elf.irelplt;
5627 }
5628 if (SYMBOL_REFERENCES_LOCAL (info, h))
5629 {
5630 bfd_put_32 (output_bfd,
5631 (h->root.u.def.value
5632 + h->root.u.def.section->output_section->vma
5633 + h->root.u.def.section->output_offset),
5634 htab->elf.sgot->contents + h->got.offset);
5635 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
5636 }
5637 else
5638 goto do_glob_dat;
5639 }
5640 else if (bfd_link_pic (info))
5641 {
5642 /* Generate R_386_GLOB_DAT. */
5643 goto do_glob_dat;
5644 }
5645 else
5646 {
5647 asection *plt;
5648
5649 if (!h->pointer_equality_needed)
5650 abort ();
5651
5652 /* For non-shared object, we can't use .got.plt, which
5653 contains the real function addres if we need pointer
5654 equality. We load the GOT entry with the PLT entry. */
5655 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
5656 bfd_put_32 (output_bfd,
5657 (plt->output_section->vma
5658 + plt->output_offset + h->plt.offset),
5659 htab->elf.sgot->contents + h->got.offset);
5660 return TRUE;
5661 }
5662 }
5663 else if (bfd_link_pic (info)
5664 && SYMBOL_REFERENCES_LOCAL (info, h))
5665 {
5666 BFD_ASSERT((h->got.offset & 1) != 0);
5667 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
5668 }
5669 else
5670 {
5671 BFD_ASSERT((h->got.offset & 1) == 0);
5672 do_glob_dat:
5673 bfd_put_32 (output_bfd, (bfd_vma) 0,
5674 htab->elf.sgot->contents + h->got.offset);
5675 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
5676 }
5677
5678 elf_append_rel (output_bfd, relgot, &rel);
5679 }
5680
5681 if (h->needs_copy)
5682 {
5683 Elf_Internal_Rela rel;
5684 asection *s;
5685
5686 /* This symbol needs a copy reloc. Set it up. */
5687
5688 if (h->dynindx == -1
5689 || (h->root.type != bfd_link_hash_defined
5690 && h->root.type != bfd_link_hash_defweak)
5691 || htab->elf.srelbss == NULL
5692 || htab->elf.sreldynrelro == NULL)
5693 abort ();
5694
5695 rel.r_offset = (h->root.u.def.value
5696 + h->root.u.def.section->output_section->vma
5697 + h->root.u.def.section->output_offset);
5698 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
5699 if (h->root.u.def.section == htab->elf.sdynrelro)
5700 s = htab->elf.sreldynrelro;
5701 else
5702 s = htab->elf.srelbss;
5703 elf_append_rel (output_bfd, s, &rel);
5704 }
5705
5706 return TRUE;
5707 }
5708
5709 /* Finish up local dynamic symbol handling. We set the contents of
5710 various dynamic sections here. */
5711
5712 static bfd_boolean
5713 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
5714 {
5715 struct elf_link_hash_entry *h
5716 = (struct elf_link_hash_entry *) *slot;
5717 struct bfd_link_info *info
5718 = (struct bfd_link_info *) inf;
5719
5720 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
5721 h, NULL);
5722 }
5723
5724 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
5725 here since undefined weak symbol may not be dynamic and may not be
5726 called for elf_i386_finish_dynamic_symbol. */
5727
5728 static bfd_boolean
5729 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
5730 void *inf)
5731 {
5732 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
5733 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5734
5735 if (h->root.type != bfd_link_hash_undefweak
5736 || h->dynindx != -1)
5737 return TRUE;
5738
5739 return elf_i386_finish_dynamic_symbol (info->output_bfd,
5740 info, h, NULL);
5741 }
5742
5743 /* Used to decide how to sort relocs in an optimal manner for the
5744 dynamic linker, before writing them out. */
5745
5746 static enum elf_reloc_type_class
5747 elf_i386_reloc_type_class (const struct bfd_link_info *info,
5748 const asection *rel_sec ATTRIBUTE_UNUSED,
5749 const Elf_Internal_Rela *rela)
5750 {
5751 bfd *abfd = info->output_bfd;
5752 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5753 struct elf_link_hash_table *htab = elf_hash_table (info);
5754
5755 if (htab->dynsym != NULL
5756 && htab->dynsym->contents != NULL)
5757 {
5758 /* Check relocation against STT_GNU_IFUNC symbol if there are
5759 dynamic symbols. */
5760 unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
5761 if (r_symndx != STN_UNDEF)
5762 {
5763 Elf_Internal_Sym sym;
5764 if (!bed->s->swap_symbol_in (abfd,
5765 (htab->dynsym->contents
5766 + r_symndx * sizeof (Elf32_External_Sym)),
5767 0, &sym))
5768 abort ();
5769
5770 if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
5771 return reloc_class_ifunc;
5772 }
5773 }
5774
5775 switch (ELF32_R_TYPE (rela->r_info))
5776 {
5777 case R_386_IRELATIVE:
5778 return reloc_class_ifunc;
5779 case R_386_RELATIVE:
5780 return reloc_class_relative;
5781 case R_386_JUMP_SLOT:
5782 return reloc_class_plt;
5783 case R_386_COPY:
5784 return reloc_class_copy;
5785 default:
5786 return reloc_class_normal;
5787 }
5788 }
5789
5790 /* Finish up the dynamic sections. */
5791
5792 static bfd_boolean
5793 elf_i386_finish_dynamic_sections (bfd *output_bfd,
5794 struct bfd_link_info *info)
5795 {
5796 struct elf_i386_link_hash_table *htab;
5797 bfd *dynobj;
5798 asection *sdyn;
5799 const struct elf_i386_backend_data *abed;
5800
5801 htab = elf_i386_hash_table (info);
5802 if (htab == NULL)
5803 return FALSE;
5804
5805 dynobj = htab->elf.dynobj;
5806 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5807 abed = get_elf_i386_backend_data (output_bfd);
5808
5809 if (htab->elf.dynamic_sections_created)
5810 {
5811 Elf32_External_Dyn *dyncon, *dynconend;
5812
5813 if (sdyn == NULL || htab->elf.sgot == NULL)
5814 abort ();
5815
5816 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5817 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5818 for (; dyncon < dynconend; dyncon++)
5819 {
5820 Elf_Internal_Dyn dyn;
5821 asection *s;
5822
5823 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5824
5825 switch (dyn.d_tag)
5826 {
5827 default:
5828 if (abed->is_vxworks
5829 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
5830 break;
5831 continue;
5832
5833 case DT_PLTGOT:
5834 s = htab->elf.sgotplt;
5835 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5836 break;
5837
5838 case DT_JMPREL:
5839 s = htab->elf.srelplt;
5840 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5841 break;
5842
5843 case DT_PLTRELSZ:
5844 s = htab->elf.srelplt;
5845 dyn.d_un.d_val = s->size;
5846 break;
5847 }
5848
5849 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5850 }
5851
5852 /* Fill in the first entry in the procedure linkage table. */
5853 if (htab->elf.splt && htab->elf.splt->size > 0)
5854 {
5855 if (bfd_link_pic (info))
5856 {
5857 memcpy (htab->elf.splt->contents, abed->plt->pic_plt0_entry,
5858 abed->plt->plt0_entry_size);
5859 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
5860 abed->plt0_pad_byte,
5861 abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
5862 }
5863 else
5864 {
5865 memcpy (htab->elf.splt->contents, abed->plt->plt0_entry,
5866 abed->plt->plt0_entry_size);
5867 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
5868 abed->plt0_pad_byte,
5869 abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
5870 bfd_put_32 (output_bfd,
5871 (htab->elf.sgotplt->output_section->vma
5872 + htab->elf.sgotplt->output_offset
5873 + 4),
5874 htab->elf.splt->contents
5875 + abed->plt->plt0_got1_offset);
5876 bfd_put_32 (output_bfd,
5877 (htab->elf.sgotplt->output_section->vma
5878 + htab->elf.sgotplt->output_offset
5879 + 8),
5880 htab->elf.splt->contents
5881 + abed->plt->plt0_got2_offset);
5882
5883 if (abed->is_vxworks)
5884 {
5885 Elf_Internal_Rela rel;
5886
5887 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
5888 On IA32 we use REL relocations so the addend goes in
5889 the PLT directly. */
5890 rel.r_offset = (htab->elf.splt->output_section->vma
5891 + htab->elf.splt->output_offset
5892 + abed->plt->plt0_got1_offset);
5893 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5894 bfd_elf32_swap_reloc_out (output_bfd, &rel,
5895 htab->srelplt2->contents);
5896 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
5897 rel.r_offset = (htab->elf.splt->output_section->vma
5898 + htab->elf.splt->output_offset
5899 + abed->plt->plt0_got2_offset);
5900 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5901 bfd_elf32_swap_reloc_out (output_bfd, &rel,
5902 htab->srelplt2->contents +
5903 sizeof (Elf32_External_Rel));
5904 }
5905 }
5906
5907 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5908 really seem like the right value. */
5909 elf_section_data (htab->elf.splt->output_section)
5910 ->this_hdr.sh_entsize = 4;
5911
5912 /* Correct the .rel.plt.unloaded relocations. */
5913 if (abed->is_vxworks && !bfd_link_pic (info))
5914 {
5915 int num_plts = (htab->elf.splt->size
5916 / abed->plt->plt_entry_size) - 1;
5917 unsigned char *p;
5918
5919 p = htab->srelplt2->contents;
5920 if (bfd_link_pic (info))
5921 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
5922 else
5923 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
5924
5925 for (; num_plts; num_plts--)
5926 {
5927 Elf_Internal_Rela rel;
5928 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
5929 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5930 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
5931 p += sizeof (Elf32_External_Rel);
5932
5933 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
5934 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
5935 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
5936 p += sizeof (Elf32_External_Rel);
5937 }
5938 }
5939 }
5940 }
5941
5942 if (htab->elf.sgotplt)
5943 {
5944 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
5945 {
5946 _bfd_error_handler
5947 (_("discarded output section: `%A'"), htab->elf.sgotplt);
5948 return FALSE;
5949 }
5950
5951 /* Fill in the first three entries in the global offset table. */
5952 if (htab->elf.sgotplt->size > 0)
5953 {
5954 bfd_put_32 (output_bfd,
5955 (sdyn == NULL ? 0
5956 : sdyn->output_section->vma + sdyn->output_offset),
5957 htab->elf.sgotplt->contents);
5958 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
5959 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
5960 }
5961
5962 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
5963 }
5964
5965 /* Adjust .eh_frame for .plt section. */
5966 if (htab->plt_eh_frame != NULL
5967 && htab->plt_eh_frame->contents != NULL)
5968 {
5969 if (htab->elf.splt != NULL
5970 && htab->elf.splt->size != 0
5971 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
5972 && htab->elf.splt->output_section != NULL
5973 && htab->plt_eh_frame->output_section != NULL)
5974 {
5975 bfd_vma plt_start = htab->elf.splt->output_section->vma;
5976 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
5977 + htab->plt_eh_frame->output_offset
5978 + PLT_FDE_START_OFFSET;
5979 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5980 htab->plt_eh_frame->contents
5981 + PLT_FDE_START_OFFSET);
5982 }
5983 if (htab->plt_eh_frame->sec_info_type
5984 == SEC_INFO_TYPE_EH_FRAME)
5985 {
5986 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5987 htab->plt_eh_frame,
5988 htab->plt_eh_frame->contents))
5989 return FALSE;
5990 }
5991 }
5992
5993 /* Adjust .eh_frame for .plt.got section. */
5994 if (htab->plt_got_eh_frame != NULL
5995 && htab->plt_got_eh_frame->contents != NULL)
5996 {
5997 if (htab->plt_got != NULL
5998 && htab->plt_got->size != 0
5999 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
6000 && htab->plt_got->output_section != NULL
6001 && htab->plt_got_eh_frame->output_section != NULL)
6002 {
6003 bfd_vma plt_start = htab->plt_got->output_section->vma;
6004 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
6005 + htab->plt_got_eh_frame->output_offset
6006 + PLT_FDE_START_OFFSET;
6007 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6008 htab->plt_got_eh_frame->contents
6009 + PLT_FDE_START_OFFSET);
6010 }
6011 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
6012 {
6013 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6014 htab->plt_got_eh_frame,
6015 htab->plt_got_eh_frame->contents))
6016 return FALSE;
6017 }
6018 }
6019
6020 if (htab->elf.sgot && htab->elf.sgot->size > 0)
6021 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
6022
6023 /* Fill PLT entries for undefined weak symbols in PIE. */
6024 if (bfd_link_pie (info))
6025 bfd_hash_traverse (&info->hash->table,
6026 elf_i386_pie_finish_undefweak_symbol,
6027 info);
6028
6029 return TRUE;
6030 }
6031
6032 /* Fill PLT/GOT entries and allocate dynamic relocations for local
6033 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
6034 It has to be done before elf_link_sort_relocs is called so that
6035 dynamic relocations are properly sorted. */
6036
6037 static bfd_boolean
6038 elf_i386_output_arch_local_syms
6039 (bfd *output_bfd ATTRIBUTE_UNUSED,
6040 struct bfd_link_info *info,
6041 void *flaginfo ATTRIBUTE_UNUSED,
6042 int (*func) (void *, const char *,
6043 Elf_Internal_Sym *,
6044 asection *,
6045 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
6046 {
6047 struct elf_i386_link_hash_table *htab = elf_i386_hash_table (info);
6048 if (htab == NULL)
6049 return FALSE;
6050
6051 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
6052 htab_traverse (htab->loc_hash_table,
6053 elf_i386_finish_local_dynamic_symbol,
6054 info);
6055
6056 return TRUE;
6057 }
6058
6059 /* Return an array of PLT entry symbol values. */
6060
6061 static bfd_vma *
6062 elf_i386_get_plt_sym_val (bfd *abfd, asymbol **dynsyms, asection *plt,
6063 asection *relplt)
6064 {
6065 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6066 arelent *p;
6067 long count, i;
6068 bfd_vma *plt_sym_val;
6069 bfd_vma plt_offset;
6070 bfd_byte *plt_contents;
6071 const struct elf_i386_backend_data *bed
6072 = get_elf_i386_backend_data (abfd);
6073 Elf_Internal_Shdr *hdr;
6074
6075 /* Get the .plt section contents. */
6076 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
6077 if (plt_contents == NULL)
6078 return NULL;
6079 if (!bfd_get_section_contents (abfd, (asection *) plt,
6080 plt_contents, 0, plt->size))
6081 {
6082 bad_return:
6083 free (plt_contents);
6084 return NULL;
6085 }
6086
6087 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6088 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
6089 goto bad_return;
6090
6091 hdr = &elf_section_data (relplt)->this_hdr;
6092 count = relplt->size / hdr->sh_entsize;
6093
6094 plt_sym_val = (bfd_vma *) bfd_malloc (sizeof (bfd_vma) * count);
6095 if (plt_sym_val == NULL)
6096 goto bad_return;
6097
6098 for (i = 0; i < count; i++)
6099 plt_sym_val[i] = -1;
6100
6101 plt_offset = bed->plt->plt_entry_size;
6102 p = relplt->relocation;
6103 for (i = 0; i < count; i++, p++)
6104 {
6105 long reloc_index;
6106
6107 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
6108 if (p->howto == NULL)
6109 continue;
6110
6111 if (p->howto->type != R_386_JUMP_SLOT
6112 && p->howto->type != R_386_IRELATIVE)
6113 continue;
6114
6115 reloc_index = H_GET_32 (abfd, (plt_contents + plt_offset
6116 + bed->plt->plt_reloc_offset));
6117 reloc_index /= sizeof (Elf32_External_Rel);
6118 if (reloc_index < count)
6119 plt_sym_val[reloc_index] = plt->vma + plt_offset;
6120
6121 plt_offset += bed->plt->plt_entry_size;
6122
6123 /* PR binutils/18437: Skip extra relocations in the .rel.plt
6124 section. */
6125 if (plt_offset >= plt->size)
6126 break;
6127 }
6128
6129 free (plt_contents);
6130
6131 return plt_sym_val;
6132 }
6133
6134 /* Similar to _bfd_elf_get_synthetic_symtab. */
6135
6136 static long
6137 elf_i386_get_synthetic_symtab (bfd *abfd,
6138 long symcount,
6139 asymbol **syms,
6140 long dynsymcount,
6141 asymbol **dynsyms,
6142 asymbol **ret)
6143 {
6144 asection *plt = bfd_get_section_by_name (abfd, ".plt");
6145 return _bfd_elf_ifunc_get_synthetic_symtab (abfd, symcount, syms,
6146 dynsymcount, dynsyms, ret,
6147 plt,
6148 elf_i386_get_plt_sym_val);
6149 }
6150
6151 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6152
6153 static bfd_boolean
6154 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
6155 {
6156 if (h->plt.offset != (bfd_vma) -1
6157 && !h->def_regular
6158 && !h->pointer_equality_needed)
6159 return FALSE;
6160
6161 return _bfd_elf_hash_symbol (h);
6162 }
6163
6164 /* Parse i386 GNU properties. */
6165
6166 static enum elf_property_kind
6167 elf_i386_parse_gnu_properties (bfd *abfd, unsigned int type,
6168 bfd_byte *ptr, unsigned int datasz)
6169 {
6170 elf_property *prop;
6171
6172 switch (type)
6173 {
6174 case GNU_PROPERTY_X86_ISA_1_USED:
6175 case GNU_PROPERTY_X86_ISA_1_NEEDED:
6176 if (datasz != 4)
6177 {
6178 _bfd_error_handler
6179 ((type == GNU_PROPERTY_X86_ISA_1_USED
6180 ? _("error: %B: <corrupt x86 ISA used size: 0x%x>")
6181 : _("error: %B: <corrupt x86 ISA needed size: 0x%x>")),
6182 abfd, datasz);
6183 return property_corrupt;
6184 }
6185 prop = _bfd_elf_get_property (abfd, type, datasz);
6186 prop->u.number = bfd_h_get_32 (abfd, ptr);
6187 prop->pr_kind = property_number;
6188 break;
6189
6190 default:
6191 return property_ignored;
6192 }
6193
6194 return property_number;
6195 }
6196
6197 /* Merge i386 GNU property BPROP with APROP. If APROP isn't NULL,
6198 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
6199 should be merged with ABFD. */
6200
6201 static bfd_boolean
6202 elf_i386_merge_gnu_properties (bfd *abfd ATTRIBUTE_UNUSED,
6203 elf_property *aprop,
6204 elf_property *bprop)
6205 {
6206 unsigned int number;
6207 bfd_boolean updated = FALSE;
6208 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
6209
6210 switch (pr_type)
6211 {
6212 case GNU_PROPERTY_X86_ISA_1_USED:
6213 case GNU_PROPERTY_X86_ISA_1_NEEDED:
6214 if (aprop != NULL && bprop != NULL)
6215 {
6216 number = aprop->u.number;
6217 aprop->u.number = number | bprop->u.number;
6218 updated = number != (unsigned int) aprop->u.number;
6219 }
6220 else
6221 {
6222 /* Return TRUE if APROP is NULL to indicate that BPROP should
6223 be added to ABFD. */
6224 updated = aprop == NULL;
6225 }
6226 break;
6227
6228 default:
6229 /* Never should happen. */
6230 abort ();
6231 }
6232
6233 return updated;
6234 }
6235
6236 #define TARGET_LITTLE_SYM i386_elf32_vec
6237 #define TARGET_LITTLE_NAME "elf32-i386"
6238 #define ELF_ARCH bfd_arch_i386
6239 #define ELF_TARGET_ID I386_ELF_DATA
6240 #define ELF_MACHINE_CODE EM_386
6241 #define ELF_MAXPAGESIZE 0x1000
6242
6243 #define elf_backend_can_gc_sections 1
6244 #define elf_backend_can_refcount 1
6245 #define elf_backend_want_got_plt 1
6246 #define elf_backend_plt_readonly 1
6247 #define elf_backend_want_plt_sym 0
6248 #define elf_backend_got_header_size 12
6249 #define elf_backend_plt_alignment 4
6250 #define elf_backend_dtrel_excludes_plt 1
6251 #define elf_backend_extern_protected_data 1
6252 #define elf_backend_caches_rawsize 1
6253 #define elf_backend_want_dynrelro 1
6254
6255 /* Support RELA for objdump of prelink objects. */
6256 #define elf_info_to_howto elf_i386_info_to_howto_rel
6257 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
6258
6259 #define bfd_elf32_mkobject elf_i386_mkobject
6260
6261 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
6262 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
6263 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
6264 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
6265 #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
6266
6267 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
6268 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
6269 #define elf_backend_check_relocs elf_i386_check_relocs
6270 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
6271 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
6272 #define elf_backend_fake_sections elf_i386_fake_sections
6273 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
6274 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
6275 #define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms
6276 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
6277 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
6278 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
6279 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
6280 #define elf_backend_relocate_section elf_i386_relocate_section
6281 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
6282 #define elf_backend_always_size_sections elf_i386_always_size_sections
6283 #define elf_backend_omit_section_dynsym \
6284 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
6285 #define elf_backend_hash_symbol elf_i386_hash_symbol
6286 #define elf_backend_fixup_symbol elf_i386_fixup_symbol
6287 #define elf_backend_parse_gnu_properties elf_i386_parse_gnu_properties
6288 #define elf_backend_merge_gnu_properties elf_i386_merge_gnu_properties
6289
6290 #include "elf32-target.h"
6291
6292 /* FreeBSD support. */
6293
6294 #undef TARGET_LITTLE_SYM
6295 #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
6296 #undef TARGET_LITTLE_NAME
6297 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
6298 #undef ELF_OSABI
6299 #define ELF_OSABI ELFOSABI_FREEBSD
6300
6301 /* The kernel recognizes executables as valid only if they carry a
6302 "FreeBSD" label in the ELF header. So we put this label on all
6303 executables and (for simplicity) also all other object files. */
6304
6305 static void
6306 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
6307 {
6308 _bfd_elf_post_process_headers (abfd, info);
6309
6310 #ifdef OLD_FREEBSD_ABI_LABEL
6311 {
6312 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
6313 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6314 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
6315 }
6316 #endif
6317 }
6318
6319 #undef elf_backend_post_process_headers
6320 #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
6321 #undef elf32_bed
6322 #define elf32_bed elf32_i386_fbsd_bed
6323
6324 #undef elf_backend_add_symbol_hook
6325
6326 #include "elf32-target.h"
6327
6328 /* Solaris 2. */
6329
6330 #undef TARGET_LITTLE_SYM
6331 #define TARGET_LITTLE_SYM i386_elf32_sol2_vec
6332 #undef TARGET_LITTLE_NAME
6333 #define TARGET_LITTLE_NAME "elf32-i386-sol2"
6334
6335 #undef elf_backend_post_process_headers
6336
6337 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
6338 objects won't be recognized. */
6339 #undef ELF_OSABI
6340
6341 #undef elf32_bed
6342 #define elf32_bed elf32_i386_sol2_bed
6343
6344 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
6345 boundary. */
6346 #undef elf_backend_static_tls_alignment
6347 #define elf_backend_static_tls_alignment 8
6348
6349 /* The Solaris 2 ABI requires a plt symbol on all platforms.
6350
6351 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
6352 File, p.63. */
6353 #undef elf_backend_want_plt_sym
6354 #define elf_backend_want_plt_sym 1
6355
6356 #undef elf_backend_strtab_flags
6357 #define elf_backend_strtab_flags SHF_STRINGS
6358
6359 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
6360 has a type >= SHT_LOOS. Returns TRUE if these fields were initialised
6361 FALSE otherwise. ISECTION is the best guess matching section from the
6362 input bfd IBFD, but it might be NULL. */
6363
6364 static bfd_boolean
6365 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
6366 bfd *obfd ATTRIBUTE_UNUSED,
6367 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
6368 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
6369 {
6370 /* PR 19938: FIXME: Need to add code for setting the sh_info
6371 and sh_link fields of Solaris specific section types. */
6372 return FALSE;
6373
6374 /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
6375 Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation:
6376
6377 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
6378
6379 The following values should be set:
6380
6381 Type Link Info
6382 -----------------------------------------------------------------------------
6383 SHT_SUNW_ancillary The section header index of 0
6384 [0x6fffffee] the associated string table.
6385
6386 SHT_SUNW_capinfo The section header index of For a dynamic object, the
6387 [0x6ffffff0] the associated symbol table. section header index of
6388 the associated
6389 SHT_SUNW_capchain table,
6390 otherwise 0.
6391
6392 SHT_SUNW_symsort The section header index of 0
6393 [0x6ffffff1] the associated symbol table.
6394
6395 SHT_SUNW_tlssort The section header index of 0
6396 [0x6ffffff2] the associated symbol table.
6397
6398 SHT_SUNW_LDYNSYM The section header index of One greater than the
6399 [0x6ffffff3] the associated string table. symbol table index of the
6400 This index is the same string last local symbol,
6401 table used by the SHT_DYNSYM STB_LOCAL. Since
6402 section. SHT_SUNW_LDYNSYM only
6403 contains local symbols,
6404 sh_info is equivalent to
6405 the number of symbols in
6406 the table.
6407
6408 SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer
6409 [0x6ffffff5] the section header index of to named strings, the
6410 the associated section header index of
6411 SHT_SUNW_capinfo table, the associated string
6412 otherwise 0. table, otherwise 0.
6413
6414 SHT_SUNW_move The section header index of 0
6415 [0x6ffffffa] the associated symbol table.
6416
6417 SHT_SUNW_COMDAT 0 0
6418 [0x6ffffffb]
6419
6420 SHT_SUNW_syminfo The section header index of The section header index
6421 [0x6ffffffc] the associated symbol table. of the associated
6422 .dynamic section.
6423
6424 SHT_SUNW_verdef The section header index of The number of version
6425 [0x6ffffffd] the associated string table. definitions within the
6426 section.
6427
6428 SHT_SUNW_verneed The section header index of The number of version
6429 [0x6ffffffe] the associated string table. dependencies within the
6430 section.
6431
6432 SHT_SUNW_versym The section header index of 0
6433 [0x6fffffff] the associated symbol table. */
6434 }
6435
6436 #undef elf_backend_copy_special_section_fields
6437 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
6438
6439 #include "elf32-target.h"
6440
6441 /* Intel MCU support. */
6442
6443 static bfd_boolean
6444 elf32_iamcu_elf_object_p (bfd *abfd)
6445 {
6446 /* Set the right machine number for an IAMCU elf32 file. */
6447 bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
6448 return TRUE;
6449 }
6450
6451 #undef TARGET_LITTLE_SYM
6452 #define TARGET_LITTLE_SYM iamcu_elf32_vec
6453 #undef TARGET_LITTLE_NAME
6454 #define TARGET_LITTLE_NAME "elf32-iamcu"
6455 #undef ELF_ARCH
6456 #define ELF_ARCH bfd_arch_iamcu
6457
6458 #undef ELF_MACHINE_CODE
6459 #define ELF_MACHINE_CODE EM_IAMCU
6460
6461 #undef ELF_OSABI
6462
6463 #undef elf32_bed
6464 #define elf32_bed elf32_iamcu_bed
6465
6466 #undef elf_backend_object_p
6467 #define elf_backend_object_p elf32_iamcu_elf_object_p
6468
6469 #undef elf_backend_static_tls_alignment
6470
6471 #undef elf_backend_want_plt_sym
6472 #define elf_backend_want_plt_sym 0
6473
6474 #undef elf_backend_strtab_flags
6475 #undef elf_backend_copy_special_section_fields
6476
6477 #include "elf32-target.h"
6478
6479 /* Restore defaults. */
6480 #undef ELF_ARCH
6481 #define ELF_ARCH bfd_arch_i386
6482 #undef ELF_MACHINE_CODE
6483 #define ELF_MACHINE_CODE EM_386
6484
6485 /* Native Client support. */
6486
6487 #undef TARGET_LITTLE_SYM
6488 #define TARGET_LITTLE_SYM i386_elf32_nacl_vec
6489 #undef TARGET_LITTLE_NAME
6490 #define TARGET_LITTLE_NAME "elf32-i386-nacl"
6491 #undef elf32_bed
6492 #define elf32_bed elf32_i386_nacl_bed
6493
6494 #undef ELF_MAXPAGESIZE
6495 #define ELF_MAXPAGESIZE 0x10000
6496
6497 /* Restore defaults. */
6498 #undef ELF_OSABI
6499 #undef elf_backend_want_plt_sym
6500 #define elf_backend_want_plt_sym 0
6501 #undef elf_backend_post_process_headers
6502 #undef elf_backend_static_tls_alignment
6503
6504 /* NaCl uses substantially different PLT entries for the same effects. */
6505
6506 #undef elf_backend_plt_alignment
6507 #define elf_backend_plt_alignment 5
6508 #define NACL_PLT_ENTRY_SIZE 64
6509 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
6510
6511 static const bfd_byte elf_i386_nacl_plt0_entry[] =
6512 {
6513 0xff, 0x35, /* pushl contents of address */
6514 0, 0, 0, 0, /* replaced with address of .got + 4. */
6515 0x8b, 0x0d, /* movl contents of address, %ecx */
6516 0, 0, 0, 0, /* replaced with address of .got + 8. */
6517 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
6518 0xff, 0xe1 /* jmp *%ecx */
6519 };
6520
6521 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
6522 {
6523 0x8b, 0x0d, /* movl contents of address, %ecx */
6524 0, 0, 0, 0, /* replaced with GOT slot address. */
6525 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
6526 0xff, 0xe1, /* jmp *%ecx */
6527
6528 /* Pad to the next 32-byte boundary with nop instructions. */
6529 0x90,
6530 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6531 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6532
6533 /* Lazy GOT entries point here (32-byte aligned). */
6534 0x68, /* pushl immediate */
6535 0, 0, 0, 0, /* replaced with reloc offset. */
6536 0xe9, /* jmp relative */
6537 0, 0, 0, 0, /* replaced with offset to .plt. */
6538
6539 /* Pad to the next 32-byte boundary with nop instructions. */
6540 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6541 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6542 0x90, 0x90
6543 };
6544
6545 static const bfd_byte
6546 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
6547 {
6548 0xff, 0x73, 0x04, /* pushl 4(%ebx) */
6549 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */
6550 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */
6551 0xff, 0xe1, /* jmp *%ecx */
6552
6553 /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
6554 so pad to that size with nop instructions. */
6555 0x90, 0x90, 0x90, 0x90, 0x90, 0x90
6556 };
6557
6558 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
6559 {
6560 0x8b, 0x8b, /* movl offset(%ebx), %ecx */
6561 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
6562 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */
6563 0xff, 0xe1, /* jmp *%ecx */
6564
6565 /* Pad to the next 32-byte boundary with nop instructions. */
6566 0x90,
6567 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6568 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6569
6570 /* Lazy GOT entries point here (32-byte aligned). */
6571 0x68, /* pushl immediate */
6572 0, 0, 0, 0, /* replaced with offset into relocation table. */
6573 0xe9, /* jmp relative */
6574 0, 0, 0, 0, /* replaced with offset to start of .plt. */
6575
6576 /* Pad to the next 32-byte boundary with nop instructions. */
6577 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6578 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6579 0x90, 0x90
6580 };
6581
6582 static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
6583 {
6584 #if (PLT_CIE_LENGTH != 20 \
6585 || PLT_FDE_LENGTH != 36 \
6586 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
6587 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
6588 # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
6589 #endif
6590 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
6591 0, 0, 0, 0, /* CIE ID */
6592 1, /* CIE version */
6593 'z', 'R', 0, /* Augmentation string */
6594 1, /* Code alignment factor */
6595 0x7c, /* Data alignment factor: -4 */
6596 8, /* Return address column */
6597 1, /* Augmentation size */
6598 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
6599 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
6600 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
6601 DW_CFA_nop, DW_CFA_nop,
6602
6603 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
6604 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
6605 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
6606 0, 0, 0, 0, /* .plt size goes here */
6607 0, /* Augmentation size */
6608 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
6609 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
6610 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
6611 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
6612 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
6613 13, /* Block length */
6614 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
6615 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
6616 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
6617 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
6618 DW_CFA_nop, DW_CFA_nop
6619 };
6620
6621 static const struct elf_i386_plt_layout elf_i386_nacl_plt =
6622 {
6623 elf_i386_nacl_plt0_entry, /* plt0_entry */
6624 sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */
6625 2, /* plt0_got1_offset */
6626 8, /* plt0_got2_offset */
6627 elf_i386_nacl_plt_entry, /* plt_entry */
6628 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
6629 2, /* plt_got_offset */
6630 33, /* plt_reloc_offset */
6631 38, /* plt_plt_offset */
6632 32, /* plt_lazy_offset */
6633 elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */
6634 elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */
6635 elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */
6636 sizeof (elf_i386_nacl_eh_frame_plt),/* eh_frame_plt_size */
6637 NULL, /* eh_frame_plt_got */
6638 0, /* eh_frame_plt_got_size */
6639 };
6640
6641 static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
6642 {
6643 &elf_i386_nacl_plt, /* plt */
6644 0x90, /* plt0_pad_byte: nop insn */
6645 0, /* is_vxworks */
6646 };
6647
6648 static bfd_boolean
6649 elf32_i386_nacl_elf_object_p (bfd *abfd)
6650 {
6651 /* Set the right machine number for a NaCl i386 ELF32 file. */
6652 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
6653 return TRUE;
6654 }
6655
6656 #undef elf_backend_arch_data
6657 #define elf_backend_arch_data &elf_i386_nacl_arch_bed
6658
6659 #undef elf_backend_object_p
6660 #define elf_backend_object_p elf32_i386_nacl_elf_object_p
6661 #undef elf_backend_modify_segment_map
6662 #define elf_backend_modify_segment_map nacl_modify_segment_map
6663 #undef elf_backend_modify_program_headers
6664 #define elf_backend_modify_program_headers nacl_modify_program_headers
6665 #undef elf_backend_final_write_processing
6666 #define elf_backend_final_write_processing nacl_final_write_processing
6667
6668 #include "elf32-target.h"
6669
6670 /* Restore defaults. */
6671 #undef elf_backend_object_p
6672 #undef elf_backend_modify_segment_map
6673 #undef elf_backend_modify_program_headers
6674 #undef elf_backend_final_write_processing
6675
6676 /* VxWorks support. */
6677
6678 #undef TARGET_LITTLE_SYM
6679 #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
6680 #undef TARGET_LITTLE_NAME
6681 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
6682 #undef ELF_OSABI
6683 #undef ELF_MAXPAGESIZE
6684 #define ELF_MAXPAGESIZE 0x1000
6685 #undef elf_backend_plt_alignment
6686 #define elf_backend_plt_alignment 4
6687
6688 static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
6689 {
6690 &elf_i386_plt, /* plt */
6691 0x90, /* plt0_pad_byte */
6692 1, /* is_vxworks */
6693 };
6694
6695 #undef elf_backend_arch_data
6696 #define elf_backend_arch_data &elf_i386_vxworks_arch_bed
6697
6698 #undef elf_backend_relocs_compatible
6699 #undef elf_backend_add_symbol_hook
6700 #define elf_backend_add_symbol_hook \
6701 elf_vxworks_add_symbol_hook
6702 #undef elf_backend_link_output_symbol_hook
6703 #define elf_backend_link_output_symbol_hook \
6704 elf_vxworks_link_output_symbol_hook
6705 #undef elf_backend_emit_relocs
6706 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
6707 #undef elf_backend_final_write_processing
6708 #define elf_backend_final_write_processing \
6709 elf_vxworks_final_write_processing
6710 #undef elf_backend_static_tls_alignment
6711
6712 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
6713 define it. */
6714 #undef elf_backend_want_plt_sym
6715 #define elf_backend_want_plt_sym 1
6716
6717 #undef elf32_bed
6718 #define elf32_bed elf32_i386_vxworks_bed
6719
6720 #include "elf32-target.h"
This page took 0.308776 seconds and 4 git commands to generate.