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