ce9cf3a20586ec4ce199a50a1ba84d33bbe0d69e
[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 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
660 0, 0, 0, 0, /* CIE ID */
661 1, /* CIE version */
662 'z', 'R', 0, /* Augmentation string */
663 1, /* Code alignment factor */
664 0x7c, /* Data alignment factor */
665 8, /* Return address column */
666 1, /* Augmentation size */
667 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
668 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
669 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
670 DW_CFA_nop, DW_CFA_nop,
671
672 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
673 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
674 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
675 0, 0, 0, 0, /* .plt size goes here */
676 0, /* Augmentation size */
677 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
678 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
679 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
680 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
681 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
682 11, /* Block length */
683 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
684 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
685 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
686 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
687 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
688 };
689
690 /* .eh_frame covering the lazy .plt section with IBT-enabled. */
691
692 static const bfd_byte elf_i386_eh_frame_lazy_ibt_plt[] =
693 {
694 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
695 0, 0, 0, 0, /* CIE ID */
696 1, /* CIE version */
697 'z', 'R', 0, /* Augmentation string */
698 1, /* Code alignment factor */
699 0x7c, /* Data alignment factor */
700 8, /* Return address column */
701 1, /* Augmentation size */
702 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
703 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
704 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
705 DW_CFA_nop, DW_CFA_nop,
706
707 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
708 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
709 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
710 0, 0, 0, 0, /* .plt size goes here */
711 0, /* Augmentation size */
712 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
713 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
714 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
715 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
716 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
717 11, /* Block length */
718 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
719 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
720 DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
721 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
722 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
723 };
724
725 /* .eh_frame covering the non-lazy .plt section. */
726
727 static const bfd_byte elf_i386_eh_frame_non_lazy_plt[] =
728 {
729 #define PLT_GOT_FDE_LENGTH 16
730 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
731 0, 0, 0, 0, /* CIE ID */
732 1, /* CIE version */
733 'z', 'R', 0, /* Augmentation string */
734 1, /* Code alignment factor */
735 0x7c, /* Data alignment factor */
736 8, /* Return address column */
737 1, /* Augmentation size */
738 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
739 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
740 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
741 DW_CFA_nop, DW_CFA_nop,
742
743 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
744 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
745 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
746 0, 0, 0, 0, /* non-lazy .plt size goes here */
747 0, /* Augmentation size */
748 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
749 };
750
751 /* These are the standard parameters. */
752 static const struct elf_x86_lazy_plt_layout elf_i386_lazy_plt =
753 {
754 elf_i386_lazy_plt0_entry, /* plt0_entry */
755 sizeof (elf_i386_lazy_plt0_entry), /* plt0_entry_size */
756 elf_i386_lazy_plt_entry, /* plt_entry */
757 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
758 2, /* plt0_got1_offset */
759 8, /* plt0_got2_offset */
760 0, /* plt0_got2_insn_end */
761 2, /* plt_got_offset */
762 7, /* plt_reloc_offset */
763 12, /* plt_plt_offset */
764 0, /* plt_got_insn_size */
765 0, /* plt_plt_insn_end */
766 6, /* plt_lazy_offset */
767 elf_i386_pic_lazy_plt0_entry, /* pic_plt0_entry */
768 elf_i386_pic_lazy_plt_entry, /* pic_plt_entry */
769 elf_i386_eh_frame_lazy_plt, /* eh_frame_plt */
770 sizeof (elf_i386_eh_frame_lazy_plt) /* eh_frame_plt_size */
771 };
772
773 static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_plt =
774 {
775 elf_i386_non_lazy_plt_entry, /* plt_entry */
776 elf_i386_pic_non_lazy_plt_entry, /* pic_plt_entry */
777 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
778 2, /* plt_got_offset */
779 0, /* plt_got_insn_size */
780 elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
781 sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
782 };
783
784 static const struct elf_x86_lazy_plt_layout elf_i386_lazy_ibt_plt =
785 {
786 elf_i386_lazy_ibt_plt0_entry, /* plt0_entry */
787 sizeof (elf_i386_lazy_ibt_plt0_entry), /* plt0_entry_size */
788 elf_i386_lazy_ibt_plt_entry, /* plt_entry */
789 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
790 2, /* plt0_got1_offset */
791 8, /* plt0_got2_offset */
792 0, /* plt0_got2_insn_end */
793 4+2, /* plt_got_offset */
794 4+1, /* plt_reloc_offset */
795 4+6, /* plt_plt_offset */
796 0, /* plt_got_insn_size */
797 0, /* plt_plt_insn_end */
798 0, /* plt_lazy_offset */
799 elf_i386_pic_lazy_ibt_plt0_entry, /* pic_plt0_entry */
800 elf_i386_lazy_ibt_plt_entry, /* pic_plt_entry */
801 elf_i386_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
802 sizeof (elf_i386_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
803 };
804
805 static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_ibt_plt =
806 {
807 elf_i386_non_lazy_ibt_plt_entry, /* plt_entry */
808 elf_i386_pic_non_lazy_ibt_plt_entry,/* pic_plt_entry */
809 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
810 4+2, /* plt_got_offset */
811 0, /* plt_got_insn_size */
812 elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
813 sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
814 };
815 \f
816
817 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
818 for the PLTResolve stub and then for each PLT entry. */
819 #define PLTRESOLVE_RELOCS_SHLIB 0
820 #define PLTRESOLVE_RELOCS 2
821 #define PLT_NON_JUMP_SLOT_RELOCS 2
822
823 /* Architecture-specific backend data for i386. */
824
825 struct elf_i386_backend_data
826 {
827 /* Value used to fill the unused bytes of the first PLT entry. */
828 bfd_byte plt0_pad_byte;
829
830 /* Target system. */
831 enum
832 {
833 is_normal,
834 is_vxworks,
835 is_nacl
836 } os;
837 };
838
839 #define get_elf_i386_backend_data(abfd) \
840 ((const struct elf_i386_backend_data *) \
841 get_elf_backend_data (abfd)->arch_data)
842
843 /* These are the standard parameters. */
844 static const struct elf_i386_backend_data elf_i386_arch_bed =
845 {
846 0, /* plt0_pad_byte */
847 is_normal /* os */
848 };
849
850 #define elf_backend_arch_data &elf_i386_arch_bed
851
852 /* Return TRUE if the TLS access code sequence support transition
853 from R_TYPE. */
854
855 static bfd_boolean
856 elf_i386_check_tls_transition (asection *sec,
857 bfd_byte *contents,
858 Elf_Internal_Shdr *symtab_hdr,
859 struct elf_link_hash_entry **sym_hashes,
860 unsigned int r_type,
861 const Elf_Internal_Rela *rel,
862 const Elf_Internal_Rela *relend)
863 {
864 unsigned int val, type, reg;
865 unsigned long r_symndx;
866 struct elf_link_hash_entry *h;
867 bfd_vma offset;
868 bfd_byte *call;
869 bfd_boolean indirect_call;
870
871 offset = rel->r_offset;
872 switch (r_type)
873 {
874 case R_386_TLS_GD:
875 case R_386_TLS_LDM:
876 if (offset < 2 || (rel + 1) >= relend)
877 return FALSE;
878
879 indirect_call = FALSE;
880 call = contents + offset + 4;
881 val = *(call - 5);
882 type = *(call - 6);
883 if (r_type == R_386_TLS_GD)
884 {
885 /* Check transition from GD access model. Only
886 leal foo@tlsgd(,%ebx,1), %eax
887 call ___tls_get_addr@PLT
888 or
889 leal foo@tlsgd(%ebx) %eax
890 call ___tls_get_addr@PLT
891 nop
892 or
893 leal foo@tlsgd(%reg), %eax
894 call *___tls_get_addr@GOT(%reg)
895 which may be converted to
896 addr32 call ___tls_get_addr
897 can transit to different access model. */
898 if ((offset + 10) > sec->size
899 || (type != 0x8d && type != 0x04))
900 return FALSE;
901
902 if (type == 0x04)
903 {
904 /* leal foo@tlsgd(,%ebx,1), %eax
905 call ___tls_get_addr@PLT */
906 if (offset < 3)
907 return FALSE;
908
909 if (*(call - 7) != 0x8d
910 || val != 0x1d
911 || call[0] != 0xe8)
912 return FALSE;
913 }
914 else
915 {
916 /* This must be
917 leal foo@tlsgd(%ebx), %eax
918 call ___tls_get_addr@PLT
919 nop
920 or
921 leal foo@tlsgd(%reg), %eax
922 call *___tls_get_addr@GOT(%reg)
923 which may be converted to
924 addr32 call ___tls_get_addr
925
926 %eax can't be used as the GOT base register since it
927 is used to pass parameter to ___tls_get_addr. */
928 reg = val & 7;
929 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
930 return FALSE;
931
932 indirect_call = call[0] == 0xff;
933 if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
934 && !(call[0] == 0x67 && call[1] == 0xe8)
935 && !(indirect_call
936 && (call[1] & 0xf8) == 0x90
937 && (call[1] & 0x7) == reg))
938 return FALSE;
939 }
940 }
941 else
942 {
943 /* Check transition from LD access model. Only
944 leal foo@tlsldm(%ebx), %eax
945 call ___tls_get_addr@PLT
946 or
947 leal foo@tlsldm(%reg), %eax
948 call *___tls_get_addr@GOT(%reg)
949 which may be converted to
950 addr32 call ___tls_get_addr
951 can transit to different access model. */
952 if (type != 0x8d || (offset + 9) > sec->size)
953 return FALSE;
954
955 /* %eax can't be used as the GOT base register since it is
956 used to pass parameter to ___tls_get_addr. */
957 reg = val & 7;
958 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
959 return FALSE;
960
961 indirect_call = call[0] == 0xff;
962 if (!(reg == 3 && call[0] == 0xe8)
963 && !(call[0] == 0x67 && call[1] == 0xe8)
964 && !(indirect_call
965 && (call[1] & 0xf8) == 0x90
966 && (call[1] & 0x7) == reg))
967 return FALSE;
968 }
969
970 r_symndx = ELF32_R_SYM (rel[1].r_info);
971 if (r_symndx < symtab_hdr->sh_info)
972 return FALSE;
973
974 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
975 if (h == NULL
976 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
977 return FALSE;
978 else if (indirect_call)
979 return (ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X);
980 else
981 return (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
982 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
983
984 case R_386_TLS_IE:
985 /* Check transition from IE access model:
986 movl foo@indntpoff(%rip), %eax
987 movl foo@indntpoff(%rip), %reg
988 addl foo@indntpoff(%rip), %reg
989 */
990
991 if (offset < 1 || (offset + 4) > sec->size)
992 return FALSE;
993
994 /* Check "movl foo@tpoff(%rip), %eax" first. */
995 val = bfd_get_8 (abfd, contents + offset - 1);
996 if (val == 0xa1)
997 return TRUE;
998
999 if (offset < 2)
1000 return FALSE;
1001
1002 /* Check movl|addl foo@tpoff(%rip), %reg. */
1003 type = bfd_get_8 (abfd, contents + offset - 2);
1004 return ((type == 0x8b || type == 0x03)
1005 && (val & 0xc7) == 0x05);
1006
1007 case R_386_TLS_GOTIE:
1008 case R_386_TLS_IE_32:
1009 /* Check transition from {IE_32,GOTIE} access model:
1010 subl foo@{tpoff,gontoff}(%reg1), %reg2
1011 movl foo@{tpoff,gontoff}(%reg1), %reg2
1012 addl foo@{tpoff,gontoff}(%reg1), %reg2
1013 */
1014
1015 if (offset < 2 || (offset + 4) > sec->size)
1016 return FALSE;
1017
1018 val = bfd_get_8 (abfd, contents + offset - 1);
1019 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1020 return FALSE;
1021
1022 type = bfd_get_8 (abfd, contents + offset - 2);
1023 return type == 0x8b || type == 0x2b || type == 0x03;
1024
1025 case R_386_TLS_GOTDESC:
1026 /* Check transition from GDesc access model:
1027 leal x@tlsdesc(%ebx), %eax
1028
1029 Make sure it's a leal adding ebx to a 32-bit offset
1030 into any register, although it's probably almost always
1031 going to be eax. */
1032
1033 if (offset < 2 || (offset + 4) > sec->size)
1034 return FALSE;
1035
1036 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1037 return FALSE;
1038
1039 val = bfd_get_8 (abfd, contents + offset - 1);
1040 return (val & 0xc7) == 0x83;
1041
1042 case R_386_TLS_DESC_CALL:
1043 /* Check transition from GDesc access model:
1044 call *x@tlsdesc(%eax)
1045 */
1046 if (offset + 2 <= sec->size)
1047 {
1048 /* Make sure that it's a call *x@tlsdesc(%eax). */
1049 call = contents + offset;
1050 return call[0] == 0xff && call[1] == 0x10;
1051 }
1052
1053 return FALSE;
1054
1055 default:
1056 abort ();
1057 }
1058 }
1059
1060 /* Return TRUE if the TLS access transition is OK or no transition
1061 will be performed. Update R_TYPE if there is a transition. */
1062
1063 static bfd_boolean
1064 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1065 asection *sec, bfd_byte *contents,
1066 Elf_Internal_Shdr *symtab_hdr,
1067 struct elf_link_hash_entry **sym_hashes,
1068 unsigned int *r_type, int tls_type,
1069 const Elf_Internal_Rela *rel,
1070 const Elf_Internal_Rela *relend,
1071 struct elf_link_hash_entry *h,
1072 unsigned long r_symndx,
1073 bfd_boolean from_relocate_section)
1074 {
1075 unsigned int from_type = *r_type;
1076 unsigned int to_type = from_type;
1077 bfd_boolean check = TRUE;
1078
1079 /* Skip TLS transition for functions. */
1080 if (h != NULL
1081 && (h->type == STT_FUNC
1082 || h->type == STT_GNU_IFUNC))
1083 return TRUE;
1084
1085 switch (from_type)
1086 {
1087 case R_386_TLS_GD:
1088 case R_386_TLS_GOTDESC:
1089 case R_386_TLS_DESC_CALL:
1090 case R_386_TLS_IE_32:
1091 case R_386_TLS_IE:
1092 case R_386_TLS_GOTIE:
1093 if (bfd_link_executable (info))
1094 {
1095 if (h == NULL)
1096 to_type = R_386_TLS_LE_32;
1097 else if (from_type != R_386_TLS_IE
1098 && from_type != R_386_TLS_GOTIE)
1099 to_type = R_386_TLS_IE_32;
1100 }
1101
1102 /* When we are called from elf_i386_relocate_section, there may
1103 be additional transitions based on TLS_TYPE. */
1104 if (from_relocate_section)
1105 {
1106 unsigned int new_to_type = to_type;
1107
1108 if (bfd_link_executable (info)
1109 && h != NULL
1110 && h->dynindx == -1
1111 && (tls_type & GOT_TLS_IE))
1112 new_to_type = R_386_TLS_LE_32;
1113
1114 if (to_type == R_386_TLS_GD
1115 || to_type == R_386_TLS_GOTDESC
1116 || to_type == R_386_TLS_DESC_CALL)
1117 {
1118 if (tls_type == GOT_TLS_IE_POS)
1119 new_to_type = R_386_TLS_GOTIE;
1120 else if (tls_type & GOT_TLS_IE)
1121 new_to_type = R_386_TLS_IE_32;
1122 }
1123
1124 /* We checked the transition before when we were called from
1125 elf_i386_check_relocs. We only want to check the new
1126 transition which hasn't been checked before. */
1127 check = new_to_type != to_type && from_type == to_type;
1128 to_type = new_to_type;
1129 }
1130
1131 break;
1132
1133 case R_386_TLS_LDM:
1134 if (bfd_link_executable (info))
1135 to_type = R_386_TLS_LE_32;
1136 break;
1137
1138 default:
1139 return TRUE;
1140 }
1141
1142 /* Return TRUE if there is no transition. */
1143 if (from_type == to_type)
1144 return TRUE;
1145
1146 /* Check if the transition can be performed. */
1147 if (check
1148 && ! elf_i386_check_tls_transition (sec, contents,
1149 symtab_hdr, sym_hashes,
1150 from_type, rel, relend))
1151 {
1152 reloc_howto_type *from, *to;
1153 const char *name;
1154
1155 from = elf_i386_rtype_to_howto (abfd, from_type);
1156 to = elf_i386_rtype_to_howto (abfd, to_type);
1157
1158 if (h)
1159 name = h->root.root.string;
1160 else
1161 {
1162 struct elf_x86_link_hash_table *htab;
1163
1164 htab = elf_x86_hash_table (info, I386_ELF_DATA);
1165 if (htab == NULL)
1166 name = "*unknown*";
1167 else
1168 {
1169 Elf_Internal_Sym *isym;
1170
1171 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1172 abfd, r_symndx);
1173 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1174 }
1175 }
1176
1177 _bfd_error_handler
1178 /* xgettext:c-format */
1179 (_("%B: TLS transition from %s to %s against `%s' at %#Lx "
1180 "in section `%A' failed"),
1181 abfd, from->name, to->name, name,
1182 rel->r_offset, sec);
1183 bfd_set_error (bfd_error_bad_value);
1184 return FALSE;
1185 }
1186
1187 *r_type = to_type;
1188 return TRUE;
1189 }
1190
1191 /* With the local symbol, foo, we convert
1192 mov foo@GOT[(%reg1)], %reg2
1193 to
1194 lea foo[@GOTOFF(%reg1)], %reg2
1195 and convert
1196 call/jmp *foo@GOT[(%reg)]
1197 to
1198 nop call foo/jmp foo nop
1199 When PIC is false, convert
1200 test %reg1, foo@GOT[(%reg2)]
1201 to
1202 test $foo, %reg1
1203 and convert
1204 binop foo@GOT[(%reg1)], %reg2
1205 to
1206 binop $foo, %reg2
1207 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1208 instructions. */
1209
1210 static
1211 bfd_boolean
1212 elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1213 bfd_byte *contents,
1214 Elf_Internal_Rela *irel,
1215 struct elf_link_hash_entry *h,
1216 bfd_boolean *converted,
1217 struct bfd_link_info *link_info)
1218 {
1219 struct elf_x86_link_hash_table *htab;
1220 unsigned int opcode;
1221 unsigned int modrm;
1222 bfd_boolean baseless;
1223 Elf_Internal_Sym *isym;
1224 unsigned int addend;
1225 unsigned int nop;
1226 bfd_vma nop_offset;
1227 bfd_boolean is_pic;
1228 bfd_boolean to_reloc_32;
1229 unsigned int r_type;
1230 unsigned int r_symndx;
1231 bfd_vma roff = irel->r_offset;
1232
1233 if (roff < 2)
1234 return TRUE;
1235
1236 /* Addend for R_386_GOT32X relocations must be 0. */
1237 addend = bfd_get_32 (abfd, contents + roff);
1238 if (addend != 0)
1239 return TRUE;
1240
1241 htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
1242 is_pic = bfd_link_pic (link_info);
1243
1244 r_type = ELF32_R_TYPE (irel->r_info);
1245 r_symndx = ELF32_R_SYM (irel->r_info);
1246
1247 modrm = bfd_get_8 (abfd, contents + roff - 1);
1248 baseless = (modrm & 0xc7) == 0x5;
1249
1250 if (baseless && is_pic)
1251 {
1252 /* For PIC, disallow R_386_GOT32X without a base register
1253 since we don't know what the GOT base is. */
1254 const char *name;
1255
1256 if (h == NULL)
1257 {
1258 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd,
1259 r_symndx);
1260 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1261 }
1262 else
1263 name = h->root.root.string;
1264
1265 _bfd_error_handler
1266 /* xgettext:c-format */
1267 (_("%B: direct GOT relocation R_386_GOT32X against `%s' without base"
1268 " register can not be used when making a shared object"),
1269 abfd, name);
1270 return FALSE;
1271 }
1272
1273 opcode = bfd_get_8 (abfd, contents + roff - 2);
1274
1275 /* Convert to R_386_32 if PIC is false or there is no base
1276 register. */
1277 to_reloc_32 = !is_pic || baseless;
1278
1279 /* Try to convert R_386_GOT32X. Get the symbol referred to by the
1280 reloc. */
1281 if (h == NULL)
1282 {
1283 if (opcode == 0x0ff)
1284 /* Convert "call/jmp *foo@GOT[(%reg)]". */
1285 goto convert_branch;
1286 else
1287 /* Convert "mov foo@GOT[(%reg1)], %reg2",
1288 "test %reg1, foo@GOT(%reg2)" and
1289 "binop foo@GOT[(%reg1)], %reg2". */
1290 goto convert_load;
1291 }
1292
1293 /* Undefined weak symbol is only bound locally in executable
1294 and its reference is resolved as 0. */
1295 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info, I386_ELF_DATA, TRUE,
1296 elf_x86_hash_entry (h)))
1297 {
1298 if (opcode == 0xff)
1299 {
1300 /* No direct branch to 0 for PIC. */
1301 if (is_pic)
1302 return TRUE;
1303 else
1304 goto convert_branch;
1305 }
1306 else
1307 {
1308 /* We can convert load of address 0 to R_386_32. */
1309 to_reloc_32 = TRUE;
1310 goto convert_load;
1311 }
1312 }
1313
1314 if (opcode == 0xff)
1315 {
1316 /* We have "call/jmp *foo@GOT[(%reg)]". */
1317 if ((h->root.type == bfd_link_hash_defined
1318 || h->root.type == bfd_link_hash_defweak)
1319 && SYMBOL_REFERENCES_LOCAL (link_info, h))
1320 {
1321 /* The function is locally defined. */
1322 convert_branch:
1323 /* Convert R_386_GOT32X to R_386_PC32. */
1324 if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1325 {
1326 struct elf_x86_link_hash_entry *eh
1327 = (struct elf_x86_link_hash_entry *) h;
1328
1329 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1330 is a nop prefix. */
1331 modrm = 0xe8;
1332 /* To support TLS optimization, always use addr32 prefix
1333 for "call *___tls_get_addr@GOT(%reg)". */
1334 if (eh && eh->tls_get_addr)
1335 {
1336 nop = 0x67;
1337 nop_offset = irel->r_offset - 2;
1338 }
1339 else
1340 {
1341 nop = link_info->call_nop_byte;
1342 if (link_info->call_nop_as_suffix)
1343 {
1344 nop_offset = roff + 3;
1345 irel->r_offset -= 1;
1346 }
1347 else
1348 nop_offset = roff - 2;
1349 }
1350 }
1351 else
1352 {
1353 /* Convert to "jmp foo nop". */
1354 modrm = 0xe9;
1355 nop = NOP_OPCODE;
1356 nop_offset = roff + 3;
1357 irel->r_offset -= 1;
1358 }
1359
1360 bfd_put_8 (abfd, nop, contents + nop_offset);
1361 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1362 /* When converting to PC-relative relocation, we
1363 need to adjust addend by -4. */
1364 bfd_put_32 (abfd, -4, contents + irel->r_offset);
1365 irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1366
1367 *converted = TRUE;
1368 }
1369 }
1370 else
1371 {
1372 /* We have "mov foo@GOT[(%re1g)], %reg2",
1373 "test %reg1, foo@GOT(%reg2)" and
1374 "binop foo@GOT[(%reg1)], %reg2".
1375
1376 Avoid optimizing _DYNAMIC since ld.so may use its
1377 link-time address. */
1378 if (h == htab->elf.hdynamic)
1379 return TRUE;
1380
1381 /* def_regular is set by an assignment in a linker script in
1382 bfd_elf_record_link_assignment. start_stop is set on
1383 __start_SECNAME/__stop_SECNAME which mark section SECNAME. */
1384 if (h->start_stop
1385 || ((h->def_regular
1386 || h->root.type == bfd_link_hash_defined
1387 || h->root.type == bfd_link_hash_defweak)
1388 && SYMBOL_REFERENCES_LOCAL (link_info, h)))
1389 {
1390 convert_load:
1391 if (opcode == 0x8b)
1392 {
1393 if (to_reloc_32)
1394 {
1395 /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1396 "mov $foo, %reg2" with R_386_32. */
1397 r_type = R_386_32;
1398 modrm = 0xc0 | (modrm & 0x38) >> 3;
1399 bfd_put_8 (abfd, modrm, contents + roff - 1);
1400 opcode = 0xc7;
1401 }
1402 else
1403 {
1404 /* Convert "mov foo@GOT(%reg1), %reg2" to
1405 "lea foo@GOTOFF(%reg1), %reg2". */
1406 r_type = R_386_GOTOFF;
1407 opcode = 0x8d;
1408 }
1409 }
1410 else
1411 {
1412 /* Only R_386_32 is supported. */
1413 if (!to_reloc_32)
1414 return TRUE;
1415
1416 if (opcode == 0x85)
1417 {
1418 /* Convert "test %reg1, foo@GOT(%reg2)" to
1419 "test $foo, %reg1". */
1420 modrm = 0xc0 | (modrm & 0x38) >> 3;
1421 opcode = 0xf7;
1422 }
1423 else
1424 {
1425 /* Convert "binop foo@GOT(%reg1), %reg2" to
1426 "binop $foo, %reg2". */
1427 modrm = (0xc0
1428 | (modrm & 0x38) >> 3
1429 | (opcode & 0x3c));
1430 opcode = 0x81;
1431 }
1432 bfd_put_8 (abfd, modrm, contents + roff - 1);
1433 r_type = R_386_32;
1434 }
1435
1436 bfd_put_8 (abfd, opcode, contents + roff - 2);
1437 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1438
1439 *converted = TRUE;
1440 }
1441 }
1442
1443 return TRUE;
1444 }
1445
1446 /* Rename some of the generic section flags to better document how they
1447 are used here. */
1448 #define need_convert_load sec_flg0
1449 #define check_relocs_failed sec_flg1
1450
1451 /* Look through the relocs for a section during the first phase, and
1452 calculate needed space in the global offset table, procedure linkage
1453 table, and dynamic reloc sections. */
1454
1455 static bfd_boolean
1456 elf_i386_check_relocs (bfd *abfd,
1457 struct bfd_link_info *info,
1458 asection *sec,
1459 const Elf_Internal_Rela *relocs)
1460 {
1461 struct elf_x86_link_hash_table *htab;
1462 Elf_Internal_Shdr *symtab_hdr;
1463 struct elf_link_hash_entry **sym_hashes;
1464 const Elf_Internal_Rela *rel;
1465 const Elf_Internal_Rela *rel_end;
1466 asection *sreloc;
1467 bfd_byte *contents;
1468
1469 if (bfd_link_relocatable (info))
1470 return TRUE;
1471
1472 /* Don't do anything special with non-loaded, non-alloced sections.
1473 In particular, any relocs in such sections should not affect GOT
1474 and PLT reference counting (ie. we don't allow them to create GOT
1475 or PLT entries), there's no possibility or desire to optimize TLS
1476 relocs, and there's not much point in propagating relocs to shared
1477 libs that the dynamic linker won't relocate. */
1478 if ((sec->flags & SEC_ALLOC) == 0)
1479 return TRUE;
1480
1481 htab = elf_x86_hash_table (info, I386_ELF_DATA);
1482 if (htab == NULL)
1483 {
1484 sec->check_relocs_failed = 1;
1485 return FALSE;
1486 }
1487
1488 BFD_ASSERT (is_x86_elf (abfd, htab));
1489
1490 /* Get the section contents. */
1491 if (elf_section_data (sec)->this_hdr.contents != NULL)
1492 contents = elf_section_data (sec)->this_hdr.contents;
1493 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1494 {
1495 sec->check_relocs_failed = 1;
1496 return FALSE;
1497 }
1498
1499 symtab_hdr = &elf_symtab_hdr (abfd);
1500 sym_hashes = elf_sym_hashes (abfd);
1501
1502 sreloc = NULL;
1503
1504 rel_end = relocs + sec->reloc_count;
1505 for (rel = relocs; rel < rel_end; rel++)
1506 {
1507 unsigned int r_type;
1508 unsigned int r_symndx;
1509 struct elf_link_hash_entry *h;
1510 struct elf_x86_link_hash_entry *eh;
1511 Elf_Internal_Sym *isym;
1512 const char *name;
1513 bfd_boolean size_reloc;
1514
1515 r_symndx = ELF32_R_SYM (rel->r_info);
1516 r_type = ELF32_R_TYPE (rel->r_info);
1517
1518 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1519 {
1520 /* xgettext:c-format */
1521 _bfd_error_handler (_("%B: bad symbol index: %d"),
1522 abfd, r_symndx);
1523 goto error_return;
1524 }
1525
1526 if (r_symndx < symtab_hdr->sh_info)
1527 {
1528 /* A local symbol. */
1529 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1530 abfd, r_symndx);
1531 if (isym == NULL)
1532 goto error_return;
1533
1534 /* Check relocation against local STT_GNU_IFUNC symbol. */
1535 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1536 {
1537 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel, TRUE);
1538 if (h == NULL)
1539 goto error_return;
1540
1541 /* Fake a STT_GNU_IFUNC symbol. */
1542 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1543 isym, NULL);
1544 h->type = STT_GNU_IFUNC;
1545 h->def_regular = 1;
1546 h->ref_regular = 1;
1547 h->forced_local = 1;
1548 h->root.type = bfd_link_hash_defined;
1549 }
1550 else
1551 h = NULL;
1552 }
1553 else
1554 {
1555 isym = NULL;
1556 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1557 while (h->root.type == bfd_link_hash_indirect
1558 || h->root.type == bfd_link_hash_warning)
1559 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1560 }
1561
1562 eh = (struct elf_x86_link_hash_entry *) h;
1563 if (h != NULL)
1564 {
1565 if (r_type == R_386_GOTOFF)
1566 eh->gotoff_ref = 1;
1567
1568 /* It is referenced by a non-shared object. */
1569 h->ref_regular = 1;
1570 h->root.non_ir_ref_regular = 1;
1571
1572 if (h->type == STT_GNU_IFUNC)
1573 elf_tdata (info->output_bfd)->has_gnu_symbols
1574 |= elf_gnu_symbol_ifunc;
1575 }
1576
1577 if (! elf_i386_tls_transition (info, abfd, sec, contents,
1578 symtab_hdr, sym_hashes,
1579 &r_type, GOT_UNKNOWN,
1580 rel, rel_end, h, r_symndx, FALSE))
1581 goto error_return;
1582
1583 switch (r_type)
1584 {
1585 case R_386_TLS_LDM:
1586 htab->tls_ld_or_ldm_got.refcount += 1;
1587 goto create_got;
1588
1589 case R_386_PLT32:
1590 /* This symbol requires a procedure linkage table entry. We
1591 actually build the entry in adjust_dynamic_symbol,
1592 because this might be a case of linking PIC code which is
1593 never referenced by a dynamic object, in which case we
1594 don't need to generate a procedure linkage table entry
1595 after all. */
1596
1597 /* If this is a local symbol, we resolve it directly without
1598 creating a procedure linkage table entry. */
1599 if (h == NULL)
1600 continue;
1601
1602 eh->has_got_reloc = 1;
1603 h->needs_plt = 1;
1604 h->plt.refcount += 1;
1605 break;
1606
1607 case R_386_SIZE32:
1608 size_reloc = TRUE;
1609 goto do_size;
1610
1611 case R_386_TLS_IE_32:
1612 case R_386_TLS_IE:
1613 case R_386_TLS_GOTIE:
1614 if (!bfd_link_executable (info))
1615 info->flags |= DF_STATIC_TLS;
1616 /* Fall through */
1617
1618 case R_386_GOT32:
1619 case R_386_GOT32X:
1620 case R_386_TLS_GD:
1621 case R_386_TLS_GOTDESC:
1622 case R_386_TLS_DESC_CALL:
1623 /* This symbol requires a global offset table entry. */
1624 {
1625 int tls_type, old_tls_type;
1626
1627 switch (r_type)
1628 {
1629 default:
1630 case R_386_GOT32:
1631 case R_386_GOT32X:
1632 tls_type = GOT_NORMAL;
1633 break;
1634 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1635 case R_386_TLS_GOTDESC:
1636 case R_386_TLS_DESC_CALL:
1637 tls_type = GOT_TLS_GDESC; break;
1638 case R_386_TLS_IE_32:
1639 if (ELF32_R_TYPE (rel->r_info) == r_type)
1640 tls_type = GOT_TLS_IE_NEG;
1641 else
1642 /* If this is a GD->IE transition, we may use either of
1643 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1644 tls_type = GOT_TLS_IE;
1645 break;
1646 case R_386_TLS_IE:
1647 case R_386_TLS_GOTIE:
1648 tls_type = GOT_TLS_IE_POS; break;
1649 }
1650
1651 if (h != NULL)
1652 {
1653 h->got.refcount += 1;
1654 old_tls_type = elf_x86_hash_entry (h)->tls_type;
1655 }
1656 else
1657 {
1658 bfd_signed_vma *local_got_refcounts;
1659
1660 /* This is a global offset table entry for a local symbol. */
1661 local_got_refcounts = elf_local_got_refcounts (abfd);
1662 if (local_got_refcounts == NULL)
1663 {
1664 bfd_size_type size;
1665
1666 size = symtab_hdr->sh_info;
1667 size *= (sizeof (bfd_signed_vma)
1668 + sizeof (bfd_vma) + sizeof(char));
1669 local_got_refcounts = (bfd_signed_vma *)
1670 bfd_zalloc (abfd, size);
1671 if (local_got_refcounts == NULL)
1672 goto error_return;
1673 elf_local_got_refcounts (abfd) = local_got_refcounts;
1674 elf_x86_local_tlsdesc_gotent (abfd)
1675 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1676 elf_x86_local_got_tls_type (abfd)
1677 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1678 }
1679 local_got_refcounts[r_symndx] += 1;
1680 old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx];
1681 }
1682
1683 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1684 tls_type |= old_tls_type;
1685 /* If a TLS symbol is accessed using IE at least once,
1686 there is no point to use dynamic model for it. */
1687 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1688 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1689 || (tls_type & GOT_TLS_IE) == 0))
1690 {
1691 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1692 tls_type = old_tls_type;
1693 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1694 && GOT_TLS_GD_ANY_P (tls_type))
1695 tls_type |= old_tls_type;
1696 else
1697 {
1698 if (h)
1699 name = h->root.root.string;
1700 else
1701 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1702 NULL);
1703 _bfd_error_handler
1704 /* xgettext:c-format */
1705 (_("%B: `%s' accessed both as normal and "
1706 "thread local symbol"),
1707 abfd, name);
1708 bfd_set_error (bfd_error_bad_value);
1709 goto error_return;
1710 }
1711 }
1712
1713 if (old_tls_type != tls_type)
1714 {
1715 if (h != NULL)
1716 elf_x86_hash_entry (h)->tls_type = tls_type;
1717 else
1718 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
1719 }
1720 }
1721 /* Fall through */
1722
1723 case R_386_GOTOFF:
1724 case R_386_GOTPC:
1725 create_got:
1726 if (r_type != R_386_TLS_IE)
1727 {
1728 if (eh != NULL)
1729 eh->has_got_reloc = 1;
1730 break;
1731 }
1732 /* Fall through */
1733
1734 case R_386_TLS_LE_32:
1735 case R_386_TLS_LE:
1736 if (eh != NULL)
1737 eh->has_got_reloc = 1;
1738 if (bfd_link_executable (info))
1739 break;
1740 info->flags |= DF_STATIC_TLS;
1741 goto do_relocation;
1742
1743 case R_386_32:
1744 case R_386_PC32:
1745 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
1746 eh->has_non_got_reloc = 1;
1747 do_relocation:
1748 /* We are called after all symbols have been resolved. Only
1749 relocation against STT_GNU_IFUNC symbol must go through
1750 PLT. */
1751 if (h != NULL
1752 && (bfd_link_executable (info)
1753 || h->type == STT_GNU_IFUNC))
1754 {
1755 /* If this reloc is in a read-only section, we might
1756 need a copy reloc. We can't check reliably at this
1757 stage whether the section is read-only, as input
1758 sections have not yet been mapped to output sections.
1759 Tentatively set the flag for now, and correct in
1760 adjust_dynamic_symbol. */
1761 h->non_got_ref = 1;
1762
1763 /* We may need a .plt entry if the symbol is a function
1764 defined in a shared lib or is a STT_GNU_IFUNC function
1765 referenced from the code or read-only section. */
1766 if (!h->def_regular
1767 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
1768 h->plt.refcount += 1;
1769
1770 if (r_type == R_386_PC32)
1771 {
1772 /* Since something like ".long foo - ." may be used
1773 as pointer, make sure that PLT is used if foo is
1774 a function defined in a shared library. */
1775 if ((sec->flags & SEC_CODE) == 0)
1776 h->pointer_equality_needed = 1;
1777 else if (h->type == STT_GNU_IFUNC
1778 && bfd_link_pic (info))
1779 {
1780 _bfd_error_handler
1781 /* xgettext:c-format */
1782 (_("%B: unsupported non-PIC call to IFUNC `%s'"),
1783 abfd, h->root.root.string);
1784 bfd_set_error (bfd_error_bad_value);
1785 goto error_return;
1786 }
1787 }
1788 else
1789 {
1790 h->pointer_equality_needed = 1;
1791 /* R_386_32 can be resolved at run-time. */
1792 if (r_type == R_386_32
1793 && (sec->flags & SEC_READONLY) == 0)
1794 eh->func_pointer_refcount += 1;
1795 }
1796 }
1797
1798 size_reloc = FALSE;
1799 do_size:
1800 /* If we are creating a shared library, and this is a reloc
1801 against a global symbol, or a non PC relative reloc
1802 against a local symbol, then we need to copy the reloc
1803 into the shared library. However, if we are linking with
1804 -Bsymbolic, we do not need to copy a reloc against a
1805 global symbol which is defined in an object we are
1806 including in the link (i.e., DEF_REGULAR is set). At
1807 this point we have not seen all the input files, so it is
1808 possible that DEF_REGULAR is not set now but will be set
1809 later (it is never cleared). In case of a weak definition,
1810 DEF_REGULAR may be cleared later by a strong definition in
1811 a shared library. We account for that possibility below by
1812 storing information in the relocs_copied field of the hash
1813 table entry. A similar situation occurs when creating
1814 shared libraries and symbol visibility changes render the
1815 symbol local.
1816
1817 If on the other hand, we are creating an executable, we
1818 may need to keep relocations for symbols satisfied by a
1819 dynamic library if we manage to avoid copy relocs for the
1820 symbol.
1821
1822 Generate dynamic pointer relocation against STT_GNU_IFUNC
1823 symbol in the non-code section. */
1824 if ((bfd_link_pic (info)
1825 && (r_type != R_386_PC32
1826 || (h != NULL
1827 && (! (bfd_link_pie (info)
1828 || SYMBOLIC_BIND (info, h))
1829 || h->root.type == bfd_link_hash_defweak
1830 || !h->def_regular))))
1831 || (h != NULL
1832 && h->type == STT_GNU_IFUNC
1833 && r_type == R_386_32
1834 && (sec->flags & SEC_CODE) == 0)
1835 || (ELIMINATE_COPY_RELOCS
1836 && !bfd_link_pic (info)
1837 && h != NULL
1838 && (h->root.type == bfd_link_hash_defweak
1839 || !h->def_regular)))
1840 {
1841 struct elf_dyn_relocs *p;
1842 struct elf_dyn_relocs **head;
1843
1844 /* We must copy these reloc types into the output file.
1845 Create a reloc section in dynobj and make room for
1846 this reloc. */
1847 if (sreloc == NULL)
1848 {
1849 sreloc = _bfd_elf_make_dynamic_reloc_section
1850 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1851
1852 if (sreloc == NULL)
1853 goto error_return;
1854 }
1855
1856 /* If this is a global symbol, we count the number of
1857 relocations we need for this symbol. */
1858 if (h != NULL)
1859 {
1860 head = &eh->dyn_relocs;
1861 }
1862 else
1863 {
1864 /* Track dynamic relocs needed for local syms too.
1865 We really need local syms available to do this
1866 easily. Oh well. */
1867 void **vpp;
1868 asection *s;
1869
1870 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1871 abfd, r_symndx);
1872 if (isym == NULL)
1873 goto error_return;
1874
1875 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1876 if (s == NULL)
1877 s = sec;
1878
1879 vpp = &elf_section_data (s)->local_dynrel;
1880 head = (struct elf_dyn_relocs **)vpp;
1881 }
1882
1883 p = *head;
1884 if (p == NULL || p->sec != sec)
1885 {
1886 bfd_size_type amt = sizeof *p;
1887 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1888 amt);
1889 if (p == NULL)
1890 goto error_return;
1891 p->next = *head;
1892 *head = p;
1893 p->sec = sec;
1894 p->count = 0;
1895 p->pc_count = 0;
1896 }
1897
1898 p->count += 1;
1899 /* Count size relocation as PC-relative relocation. */
1900 if (r_type == R_386_PC32 || size_reloc)
1901 p->pc_count += 1;
1902 }
1903 break;
1904
1905 /* This relocation describes the C++ object vtable hierarchy.
1906 Reconstruct it for later use during GC. */
1907 case R_386_GNU_VTINHERIT:
1908 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1909 goto error_return;
1910 break;
1911
1912 /* This relocation describes which C++ vtable entries are actually
1913 used. Record for later use during GC. */
1914 case R_386_GNU_VTENTRY:
1915 BFD_ASSERT (h != NULL);
1916 if (h != NULL
1917 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1918 goto error_return;
1919 break;
1920
1921 default:
1922 break;
1923 }
1924
1925 if (r_type == R_386_GOT32X
1926 && (h == NULL || h->type != STT_GNU_IFUNC))
1927 sec->need_convert_load = 1;
1928 }
1929
1930 if (elf_section_data (sec)->this_hdr.contents != contents)
1931 {
1932 if (!info->keep_memory)
1933 free (contents);
1934 else
1935 {
1936 /* Cache the section contents for elf_link_input_bfd. */
1937 elf_section_data (sec)->this_hdr.contents = contents;
1938 }
1939 }
1940
1941 return TRUE;
1942
1943 error_return:
1944 if (elf_section_data (sec)->this_hdr.contents != contents)
1945 free (contents);
1946 sec->check_relocs_failed = 1;
1947 return FALSE;
1948 }
1949
1950 /* Convert load via the GOT slot to load immediate. */
1951
1952 bfd_boolean
1953 _bfd_i386_elf_convert_load (bfd *abfd, asection *sec,
1954 struct bfd_link_info *link_info)
1955 {
1956 struct elf_x86_link_hash_table *htab;
1957 Elf_Internal_Shdr *symtab_hdr;
1958 Elf_Internal_Rela *internal_relocs;
1959 Elf_Internal_Rela *irel, *irelend;
1960 bfd_byte *contents;
1961 bfd_boolean changed;
1962 bfd_signed_vma *local_got_refcounts;
1963
1964 /* Don't even try to convert non-ELF outputs. */
1965 if (!is_elf_hash_table (link_info->hash))
1966 return FALSE;
1967
1968 /* Nothing to do if there is no need or no output. */
1969 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
1970 || sec->need_convert_load == 0
1971 || bfd_is_abs_section (sec->output_section))
1972 return TRUE;
1973
1974 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1975
1976 /* Load the relocations for this section. */
1977 internal_relocs = (_bfd_elf_link_read_relocs
1978 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
1979 link_info->keep_memory));
1980 if (internal_relocs == NULL)
1981 return FALSE;
1982
1983 changed = FALSE;
1984 htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
1985 local_got_refcounts = elf_local_got_refcounts (abfd);
1986
1987 /* Get the section contents. */
1988 if (elf_section_data (sec)->this_hdr.contents != NULL)
1989 contents = elf_section_data (sec)->this_hdr.contents;
1990 else
1991 {
1992 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1993 goto error_return;
1994 }
1995
1996 irelend = internal_relocs + sec->reloc_count;
1997 for (irel = internal_relocs; irel < irelend; irel++)
1998 {
1999 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
2000 unsigned int r_symndx;
2001 struct elf_link_hash_entry *h;
2002 bfd_boolean converted;
2003
2004 /* Don't convert R_386_GOT32 since we can't tell if it is applied
2005 to "mov $foo@GOT, %reg" which isn't a load via GOT. */
2006 if (r_type != R_386_GOT32X)
2007 continue;
2008
2009 r_symndx = ELF32_R_SYM (irel->r_info);
2010 if (r_symndx < symtab_hdr->sh_info)
2011 h = _bfd_elf_x86_get_local_sym_hash (htab, sec->owner,
2012 (const Elf_Internal_Rela *) irel,
2013 FALSE);
2014 else
2015 {
2016 h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
2017 while (h->root.type == bfd_link_hash_indirect
2018 || h->root.type == bfd_link_hash_warning)
2019 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2020 }
2021
2022 /* STT_GNU_IFUNC must keep GOT32 relocations. */
2023 if (h != NULL && h->type == STT_GNU_IFUNC)
2024 continue;
2025
2026 converted = FALSE;
2027 if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
2028 irel, h, &converted, link_info))
2029 goto error_return;
2030
2031 if (converted)
2032 {
2033 changed = converted;
2034 if (h)
2035 {
2036 if (h->got.refcount > 0)
2037 h->got.refcount -= 1;
2038 }
2039 else
2040 {
2041 if (local_got_refcounts != NULL
2042 && local_got_refcounts[r_symndx] > 0)
2043 local_got_refcounts[r_symndx] -= 1;
2044 }
2045 }
2046 }
2047
2048 if (contents != NULL
2049 && elf_section_data (sec)->this_hdr.contents != contents)
2050 {
2051 if (!changed && !link_info->keep_memory)
2052 free (contents);
2053 else
2054 {
2055 /* Cache the section contents for elf_link_input_bfd. */
2056 elf_section_data (sec)->this_hdr.contents = contents;
2057 }
2058 }
2059
2060 if (elf_section_data (sec)->relocs != internal_relocs)
2061 {
2062 if (!changed)
2063 free (internal_relocs);
2064 else
2065 elf_section_data (sec)->relocs = internal_relocs;
2066 }
2067
2068 return TRUE;
2069
2070 error_return:
2071 if (contents != NULL
2072 && elf_section_data (sec)->this_hdr.contents != contents)
2073 free (contents);
2074 if (internal_relocs != NULL
2075 && elf_section_data (sec)->relocs != internal_relocs)
2076 free (internal_relocs);
2077 return FALSE;
2078 }
2079
2080 /* Set the correct type for an x86 ELF section. We do this by the
2081 section name, which is a hack, but ought to work. */
2082
2083 static bfd_boolean
2084 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2085 Elf_Internal_Shdr *hdr,
2086 asection *sec)
2087 {
2088 const char *name;
2089
2090 name = bfd_get_section_name (abfd, sec);
2091
2092 /* This is an ugly, but unfortunately necessary hack that is
2093 needed when producing EFI binaries on x86. It tells
2094 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2095 containing ELF relocation info. We need this hack in order to
2096 be able to generate ELF binaries that can be translated into
2097 EFI applications (which are essentially COFF objects). Those
2098 files contain a COFF ".reloc" section inside an ELFNN object,
2099 which would normally cause BFD to segfault because it would
2100 attempt to interpret this section as containing relocation
2101 entries for section "oc". With this hack enabled, ".reloc"
2102 will be treated as a normal data section, which will avoid the
2103 segfault. However, you won't be able to create an ELFNN binary
2104 with a section named "oc" that needs relocations, but that's
2105 the kind of ugly side-effects you get when detecting section
2106 types based on their names... In practice, this limitation is
2107 unlikely to bite. */
2108 if (strcmp (name, ".reloc") == 0)
2109 hdr->sh_type = SHT_PROGBITS;
2110
2111 return TRUE;
2112 }
2113
2114 /* Return the relocation value for @tpoff relocation
2115 if STT_TLS virtual address is ADDRESS. */
2116
2117 static bfd_vma
2118 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
2119 {
2120 struct elf_link_hash_table *htab = elf_hash_table (info);
2121 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2122 bfd_vma static_tls_size;
2123
2124 /* If tls_sec is NULL, we should have signalled an error already. */
2125 if (htab->tls_sec == NULL)
2126 return 0;
2127
2128 /* Consider special static TLS alignment requirements. */
2129 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2130 return static_tls_size + htab->tls_sec->vma - address;
2131 }
2132
2133 /* Relocate an i386 ELF section. */
2134
2135 static bfd_boolean
2136 elf_i386_relocate_section (bfd *output_bfd,
2137 struct bfd_link_info *info,
2138 bfd *input_bfd,
2139 asection *input_section,
2140 bfd_byte *contents,
2141 Elf_Internal_Rela *relocs,
2142 Elf_Internal_Sym *local_syms,
2143 asection **local_sections)
2144 {
2145 struct elf_x86_link_hash_table *htab;
2146 Elf_Internal_Shdr *symtab_hdr;
2147 struct elf_link_hash_entry **sym_hashes;
2148 bfd_vma *local_got_offsets;
2149 bfd_vma *local_tlsdesc_gotents;
2150 Elf_Internal_Rela *rel;
2151 Elf_Internal_Rela *wrel;
2152 Elf_Internal_Rela *relend;
2153 bfd_boolean is_vxworks_tls;
2154 unsigned plt_entry_size;
2155
2156 /* Skip if check_relocs failed. */
2157 if (input_section->check_relocs_failed)
2158 return FALSE;
2159
2160 htab = elf_x86_hash_table (info, I386_ELF_DATA);
2161 if (htab == NULL)
2162 return FALSE;
2163
2164 BFD_ASSERT (is_x86_elf (input_bfd, htab));
2165
2166 symtab_hdr = &elf_symtab_hdr (input_bfd);
2167 sym_hashes = elf_sym_hashes (input_bfd);
2168 local_got_offsets = elf_local_got_offsets (input_bfd);
2169 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2170 /* We have to handle relocations in vxworks .tls_vars sections
2171 specially, because the dynamic loader is 'weird'. */
2172 is_vxworks_tls = (htab->is_vxworks
2173 && bfd_link_pic (info)
2174 && !strcmp (input_section->output_section->name,
2175 ".tls_vars"));
2176
2177 _bfd_x86_elf_set_tls_module_base (info);
2178
2179 plt_entry_size = htab->plt.plt_entry_size;
2180
2181 rel = wrel = relocs;
2182 relend = relocs + input_section->reloc_count;
2183 for (; rel < relend; wrel++, rel++)
2184 {
2185 unsigned int r_type;
2186 reloc_howto_type *howto;
2187 unsigned long r_symndx;
2188 struct elf_link_hash_entry *h;
2189 struct elf_x86_link_hash_entry *eh;
2190 Elf_Internal_Sym *sym;
2191 asection *sec;
2192 bfd_vma off, offplt, plt_offset;
2193 bfd_vma relocation;
2194 bfd_boolean unresolved_reloc;
2195 bfd_reloc_status_type r;
2196 unsigned int indx;
2197 int tls_type;
2198 bfd_vma st_size;
2199 asection *resolved_plt;
2200 bfd_boolean resolved_to_zero;
2201 bfd_boolean relative_reloc;
2202
2203 r_type = ELF32_R_TYPE (rel->r_info);
2204 if (r_type == R_386_GNU_VTINHERIT
2205 || r_type == R_386_GNU_VTENTRY)
2206 {
2207 if (wrel != rel)
2208 *wrel = *rel;
2209 continue;
2210 }
2211
2212 if ((indx = r_type) >= R_386_standard
2213 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2214 >= R_386_ext - R_386_standard)
2215 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2216 >= R_386_ext2 - R_386_ext))
2217 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2218
2219 howto = elf_howto_table + indx;
2220
2221 r_symndx = ELF32_R_SYM (rel->r_info);
2222 h = NULL;
2223 sym = NULL;
2224 sec = NULL;
2225 unresolved_reloc = FALSE;
2226 if (r_symndx < symtab_hdr->sh_info)
2227 {
2228 sym = local_syms + r_symndx;
2229 sec = local_sections[r_symndx];
2230 relocation = (sec->output_section->vma
2231 + sec->output_offset
2232 + sym->st_value);
2233 st_size = sym->st_size;
2234
2235 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2236 && ((sec->flags & SEC_MERGE) != 0
2237 || (bfd_link_relocatable (info)
2238 && sec->output_offset != 0)))
2239 {
2240 bfd_vma addend;
2241 bfd_byte *where = contents + rel->r_offset;
2242
2243 switch (howto->size)
2244 {
2245 case 0:
2246 addend = bfd_get_8 (input_bfd, where);
2247 if (howto->pc_relative)
2248 {
2249 addend = (addend ^ 0x80) - 0x80;
2250 addend += 1;
2251 }
2252 break;
2253 case 1:
2254 addend = bfd_get_16 (input_bfd, where);
2255 if (howto->pc_relative)
2256 {
2257 addend = (addend ^ 0x8000) - 0x8000;
2258 addend += 2;
2259 }
2260 break;
2261 case 2:
2262 addend = bfd_get_32 (input_bfd, where);
2263 if (howto->pc_relative)
2264 {
2265 addend = (addend ^ 0x80000000) - 0x80000000;
2266 addend += 4;
2267 }
2268 break;
2269 default:
2270 abort ();
2271 }
2272
2273 if (bfd_link_relocatable (info))
2274 addend += sec->output_offset;
2275 else
2276 {
2277 asection *msec = sec;
2278 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2279 addend);
2280 addend -= relocation;
2281 addend += msec->output_section->vma + msec->output_offset;
2282 }
2283
2284 switch (howto->size)
2285 {
2286 case 0:
2287 /* FIXME: overflow checks. */
2288 if (howto->pc_relative)
2289 addend -= 1;
2290 bfd_put_8 (input_bfd, addend, where);
2291 break;
2292 case 1:
2293 if (howto->pc_relative)
2294 addend -= 2;
2295 bfd_put_16 (input_bfd, addend, where);
2296 break;
2297 case 2:
2298 if (howto->pc_relative)
2299 addend -= 4;
2300 bfd_put_32 (input_bfd, addend, where);
2301 break;
2302 }
2303 }
2304 else if (!bfd_link_relocatable (info)
2305 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2306 {
2307 /* Relocate against local STT_GNU_IFUNC symbol. */
2308 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
2309 FALSE);
2310 if (h == NULL)
2311 abort ();
2312
2313 /* Set STT_GNU_IFUNC symbol value. */
2314 h->root.u.def.value = sym->st_value;
2315 h->root.u.def.section = sec;
2316 }
2317 }
2318 else
2319 {
2320 bfd_boolean warned ATTRIBUTE_UNUSED;
2321 bfd_boolean ignored ATTRIBUTE_UNUSED;
2322
2323 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2324 r_symndx, symtab_hdr, sym_hashes,
2325 h, sec, relocation,
2326 unresolved_reloc, warned, ignored);
2327 st_size = h->size;
2328 }
2329
2330 if (sec != NULL && discarded_section (sec))
2331 {
2332 _bfd_clear_contents (howto, input_bfd, input_section,
2333 contents + rel->r_offset);
2334 wrel->r_offset = rel->r_offset;
2335 wrel->r_info = 0;
2336 wrel->r_addend = 0;
2337
2338 /* For ld -r, remove relocations in debug sections against
2339 sections defined in discarded sections. Not done for
2340 eh_frame editing code expects to be present. */
2341 if (bfd_link_relocatable (info)
2342 && (input_section->flags & SEC_DEBUGGING))
2343 wrel--;
2344
2345 continue;
2346 }
2347
2348 if (bfd_link_relocatable (info))
2349 {
2350 if (wrel != rel)
2351 *wrel = *rel;
2352 continue;
2353 }
2354
2355 eh = (struct elf_x86_link_hash_entry *) h;
2356
2357 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2358 it here if it is defined in a non-shared object. */
2359 if (h != NULL
2360 && h->type == STT_GNU_IFUNC
2361 && h->def_regular)
2362 {
2363 asection *gotplt, *base_got;
2364 bfd_vma plt_index;
2365 const char *name;
2366
2367 if ((input_section->flags & SEC_ALLOC) == 0)
2368 {
2369 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2370 sections because such sections are not SEC_ALLOC and
2371 thus ld.so will not process them. */
2372 if ((input_section->flags & SEC_DEBUGGING) != 0)
2373 continue;
2374 abort ();
2375 }
2376
2377 /* STT_GNU_IFUNC symbol must go through PLT. */
2378 if (htab->elf.splt != NULL)
2379 {
2380 if (htab->plt_second != NULL)
2381 {
2382 resolved_plt = htab->plt_second;
2383 plt_offset = eh->plt_second.offset;
2384 }
2385 else
2386 {
2387 resolved_plt = htab->elf.splt;
2388 plt_offset = h->plt.offset;
2389 }
2390 gotplt = htab->elf.sgotplt;
2391 }
2392 else
2393 {
2394 resolved_plt = htab->elf.iplt;
2395 plt_offset = h->plt.offset;
2396 gotplt = htab->elf.igotplt;
2397 }
2398
2399 switch (r_type)
2400 {
2401 default:
2402 break;
2403
2404 case R_386_GOT32:
2405 case R_386_GOT32X:
2406 base_got = htab->elf.sgot;
2407 off = h->got.offset;
2408
2409 if (base_got == NULL)
2410 abort ();
2411
2412 if (off == (bfd_vma) -1)
2413 {
2414 /* We can't use h->got.offset here to save state, or
2415 even just remember the offset, as finish_dynamic_symbol
2416 would use that as offset into .got. */
2417
2418 if (h->plt.offset == (bfd_vma) -1)
2419 abort ();
2420
2421 if (htab->elf.splt != NULL)
2422 {
2423 plt_index = (h->plt.offset / plt_entry_size
2424 - htab->plt.has_plt0);
2425 off = (plt_index + 3) * 4;
2426 base_got = htab->elf.sgotplt;
2427 }
2428 else
2429 {
2430 plt_index = h->plt.offset / plt_entry_size;
2431 off = plt_index * 4;
2432 base_got = htab->elf.igotplt;
2433 }
2434
2435 if (h->dynindx == -1
2436 || h->forced_local
2437 || info->symbolic)
2438 {
2439 /* This references the local defitionion. We must
2440 initialize this entry in the global offset table.
2441 Since the offset must always be a multiple of 8,
2442 we use the least significant bit to record
2443 whether we have initialized it already.
2444
2445 When doing a dynamic link, we create a .rela.got
2446 relocation entry to initialize the value. This
2447 is done in the finish_dynamic_symbol routine. */
2448 if ((off & 1) != 0)
2449 off &= ~1;
2450 else
2451 {
2452 bfd_put_32 (output_bfd, relocation,
2453 base_got->contents + off);
2454 h->got.offset |= 1;
2455 }
2456 }
2457
2458 relocation = off;
2459 }
2460 else
2461 relocation = (base_got->output_section->vma
2462 + base_got->output_offset + off
2463 - gotplt->output_section->vma
2464 - gotplt->output_offset);
2465
2466 if (rel->r_offset > 1
2467 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2468 && *(contents + rel->r_offset - 2) != 0x8d)
2469 {
2470 if (bfd_link_pic (info))
2471 goto disallow_got32;
2472
2473 /* Add the GOT base if there is no base register. */
2474 relocation += (gotplt->output_section->vma
2475 + gotplt->output_offset);
2476 }
2477 else if (htab->elf.splt == NULL)
2478 {
2479 /* Adjust for static executables. */
2480 relocation += gotplt->output_offset;
2481 }
2482
2483 goto do_relocation;
2484 }
2485
2486 if (h->plt.offset == (bfd_vma) -1)
2487 {
2488 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2489 if (r_type == R_386_32
2490 && (input_section->flags & SEC_CODE) == 0)
2491 goto do_ifunc_pointer;
2492 goto bad_ifunc_reloc;
2493 }
2494
2495 relocation = (resolved_plt->output_section->vma
2496 + resolved_plt->output_offset + plt_offset);
2497
2498 switch (r_type)
2499 {
2500 default:
2501 bad_ifunc_reloc:
2502 if (h->root.root.string)
2503 name = h->root.root.string;
2504 else
2505 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2506 NULL);
2507 _bfd_error_handler
2508 /* xgettext:c-format */
2509 (_("%B: relocation %s against STT_GNU_IFUNC "
2510 "symbol `%s' isn't supported"), input_bfd,
2511 howto->name, name);
2512 bfd_set_error (bfd_error_bad_value);
2513 return FALSE;
2514
2515 case R_386_32:
2516 /* Generate dynamic relcoation only when there is a
2517 non-GOT reference in a shared object. */
2518 if ((bfd_link_pic (info) && h->non_got_ref)
2519 || h->plt.offset == (bfd_vma) -1)
2520 {
2521 Elf_Internal_Rela outrel;
2522 asection *sreloc;
2523 bfd_vma offset;
2524
2525 do_ifunc_pointer:
2526 /* Need a dynamic relocation to get the real function
2527 adddress. */
2528 offset = _bfd_elf_section_offset (output_bfd,
2529 info,
2530 input_section,
2531 rel->r_offset);
2532 if (offset == (bfd_vma) -1
2533 || offset == (bfd_vma) -2)
2534 abort ();
2535
2536 outrel.r_offset = (input_section->output_section->vma
2537 + input_section->output_offset
2538 + offset);
2539
2540 if (h->dynindx == -1
2541 || h->forced_local
2542 || bfd_link_executable (info))
2543 {
2544 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
2545 h->root.root.string,
2546 h->root.u.def.section->owner);
2547
2548 /* This symbol is resolved locally. */
2549 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
2550 bfd_put_32 (output_bfd,
2551 (h->root.u.def.value
2552 + h->root.u.def.section->output_section->vma
2553 + h->root.u.def.section->output_offset),
2554 contents + offset);
2555 }
2556 else
2557 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2558
2559 /* Dynamic relocations are stored in
2560 1. .rel.ifunc section in PIC object.
2561 2. .rel.got section in dynamic executable.
2562 3. .rel.iplt section in static executable. */
2563 if (bfd_link_pic (info))
2564 sreloc = htab->elf.irelifunc;
2565 else if (htab->elf.splt != NULL)
2566 sreloc = htab->elf.srelgot;
2567 else
2568 sreloc = htab->elf.irelplt;
2569 elf_append_rel (output_bfd, sreloc, &outrel);
2570
2571 /* If this reloc is against an external symbol, we
2572 do not want to fiddle with the addend. Otherwise,
2573 we need to include the symbol value so that it
2574 becomes an addend for the dynamic reloc. For an
2575 internal symbol, we have updated addend. */
2576 continue;
2577 }
2578 /* FALLTHROUGH */
2579 case R_386_PC32:
2580 case R_386_PLT32:
2581 goto do_relocation;
2582
2583 case R_386_GOTOFF:
2584 relocation -= (gotplt->output_section->vma
2585 + gotplt->output_offset);
2586 goto do_relocation;
2587 }
2588 }
2589
2590 resolved_to_zero = (eh != NULL
2591 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2592 I386_ELF_DATA,
2593 eh->has_got_reloc,
2594 eh));
2595
2596 switch (r_type)
2597 {
2598 case R_386_GOT32X:
2599 /* Avoid optimizing _DYNAMIC since ld.so may use its
2600 link-time address. */
2601 if (h == htab->elf.hdynamic)
2602 goto r_386_got32;
2603
2604 if (bfd_link_pic (info))
2605 {
2606 /* It is OK to convert mov to lea and convert indirect
2607 branch to direct branch. It is OK to convert adc,
2608 add, and, cmp, or, sbb, sub, test, xor only when PIC
2609 is false. */
2610 unsigned int opcode, addend;
2611 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
2612 if (addend != 0)
2613 goto r_386_got32;
2614 opcode = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2615 if (opcode != 0x8b && opcode != 0xff)
2616 goto r_386_got32;
2617 }
2618
2619 /* Resolve "mov GOT[(%reg)], %reg",
2620 "call/jmp *GOT[(%reg)]", "test %reg, foo@GOT[(%reg)]"
2621 and "binop foo@GOT[(%reg)], %reg". */
2622 if (h == NULL
2623 || (h->plt.offset == (bfd_vma) -1
2624 && h->got.offset == (bfd_vma) -1)
2625 || htab->elf.sgotplt == NULL)
2626 abort ();
2627
2628 offplt = (htab->elf.sgotplt->output_section->vma
2629 + htab->elf.sgotplt->output_offset);
2630
2631 /* It is relative to .got.plt section. */
2632 if (h->got.offset != (bfd_vma) -1)
2633 /* Use GOT entry. Mask off the least significant bit in
2634 GOT offset which may be set by R_386_GOT32 processing
2635 below. */
2636 relocation = (htab->elf.sgot->output_section->vma
2637 + htab->elf.sgot->output_offset
2638 + (h->got.offset & ~1) - offplt);
2639 else
2640 /* Use GOTPLT entry. */
2641 relocation = (h->plt.offset / plt_entry_size
2642 - htab->plt.has_plt0 + 3) * 4;
2643
2644 if (!bfd_link_pic (info))
2645 {
2646 /* If not PIC, add the .got.plt section address for
2647 baseless addressing. */
2648 unsigned int modrm;
2649 modrm = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2650 if ((modrm & 0xc7) == 0x5)
2651 relocation += offplt;
2652 }
2653
2654 unresolved_reloc = FALSE;
2655 break;
2656
2657 case R_386_GOT32:
2658 r_386_got32:
2659 /* Relocation is to the entry for this symbol in the global
2660 offset table. */
2661 if (htab->elf.sgot == NULL)
2662 abort ();
2663
2664 relative_reloc = FALSE;
2665 if (h != NULL)
2666 {
2667 bfd_boolean dyn;
2668
2669 off = h->got.offset;
2670 dyn = htab->elf.dynamic_sections_created;
2671 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2672 bfd_link_pic (info),
2673 h)
2674 || (bfd_link_pic (info)
2675 && SYMBOL_REFERENCES_LOCAL (info, h))
2676 || (ELF_ST_VISIBILITY (h->other)
2677 && h->root.type == bfd_link_hash_undefweak))
2678 {
2679 /* This is actually a static link, or it is a
2680 -Bsymbolic link and the symbol is defined
2681 locally, or the symbol was forced to be local
2682 because of a version file. We must initialize
2683 this entry in the global offset table. Since the
2684 offset must always be a multiple of 4, we use the
2685 least significant bit to record whether we have
2686 initialized it already.
2687
2688 When doing a dynamic link, we create a .rel.got
2689 relocation entry to initialize the value. This
2690 is done in the finish_dynamic_symbol routine. */
2691 if ((off & 1) != 0)
2692 off &= ~1;
2693 else
2694 {
2695 bfd_put_32 (output_bfd, relocation,
2696 htab->elf.sgot->contents + off);
2697 h->got.offset |= 1;
2698
2699 if (h->dynindx == -1
2700 && !h->forced_local
2701 && h->root.type != bfd_link_hash_undefweak
2702 && bfd_link_pic (info))
2703 {
2704 /* PR ld/21402: If this symbol isn't dynamic
2705 in PIC, generate R_386_RELATIVE here. */
2706 eh->no_finish_dynamic_symbol = 1;
2707 relative_reloc = TRUE;
2708 }
2709 }
2710 }
2711 else
2712 unresolved_reloc = FALSE;
2713 }
2714 else
2715 {
2716 if (local_got_offsets == NULL)
2717 abort ();
2718
2719 off = local_got_offsets[r_symndx];
2720
2721 /* The offset must always be a multiple of 4. We use
2722 the least significant bit to record whether we have
2723 already generated the necessary reloc. */
2724 if ((off & 1) != 0)
2725 off &= ~1;
2726 else
2727 {
2728 bfd_put_32 (output_bfd, relocation,
2729 htab->elf.sgot->contents + off);
2730 local_got_offsets[r_symndx] |= 1;
2731
2732 if (bfd_link_pic (info))
2733 relative_reloc = TRUE;
2734 }
2735 }
2736
2737 if (relative_reloc)
2738 {
2739 asection *s;
2740 Elf_Internal_Rela outrel;
2741
2742 s = htab->elf.srelgot;
2743 if (s == NULL)
2744 abort ();
2745
2746 outrel.r_offset = (htab->elf.sgot->output_section->vma
2747 + htab->elf.sgot->output_offset
2748 + off);
2749 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2750 elf_append_rel (output_bfd, s, &outrel);
2751 }
2752
2753 if (off >= (bfd_vma) -2)
2754 abort ();
2755
2756 relocation = (htab->elf.sgot->output_section->vma
2757 + htab->elf.sgot->output_offset + off);
2758 if (rel->r_offset > 1
2759 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2760 && *(contents + rel->r_offset - 2) != 0x8d)
2761 {
2762 if (bfd_link_pic (info))
2763 {
2764 /* For PIC, disallow R_386_GOT32 without a base
2765 register, except for "lea foo@GOT, %reg", since
2766 we don't know what the GOT base is. */
2767 const char *name;
2768
2769 disallow_got32:
2770 if (h == NULL || h->root.root.string == NULL)
2771 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2772 NULL);
2773 else
2774 name = h->root.root.string;
2775
2776 _bfd_error_handler
2777 /* xgettext:c-format */
2778 (_("%B: direct GOT relocation %s against `%s'"
2779 " without base register can not be used"
2780 " when making a shared object"),
2781 input_bfd, howto->name, name);
2782 bfd_set_error (bfd_error_bad_value);
2783 return FALSE;
2784 }
2785 }
2786 else
2787 {
2788 /* Subtract the .got.plt section address only with a base
2789 register. */
2790 relocation -= (htab->elf.sgotplt->output_section->vma
2791 + htab->elf.sgotplt->output_offset);
2792 }
2793
2794 break;
2795
2796 case R_386_GOTOFF:
2797 /* Relocation is relative to the start of the global offset
2798 table. */
2799
2800 /* Check to make sure it isn't a protected function or data
2801 symbol for shared library since it may not be local when
2802 used as function address or with copy relocation. We also
2803 need to make sure that a symbol is referenced locally. */
2804 if (!bfd_link_executable (info) && h)
2805 {
2806 if (!h->def_regular)
2807 {
2808 const char *v;
2809
2810 switch (ELF_ST_VISIBILITY (h->other))
2811 {
2812 case STV_HIDDEN:
2813 v = _("hidden symbol");
2814 break;
2815 case STV_INTERNAL:
2816 v = _("internal symbol");
2817 break;
2818 case STV_PROTECTED:
2819 v = _("protected symbol");
2820 break;
2821 default:
2822 v = _("symbol");
2823 break;
2824 }
2825
2826 _bfd_error_handler
2827 /* xgettext:c-format */
2828 (_("%B: relocation R_386_GOTOFF against undefined %s"
2829 " `%s' can not be used when making a shared object"),
2830 input_bfd, v, h->root.root.string);
2831 bfd_set_error (bfd_error_bad_value);
2832 return FALSE;
2833 }
2834 else if (!SYMBOL_REFERENCES_LOCAL (info, h)
2835 && (h->type == STT_FUNC
2836 || h->type == STT_OBJECT)
2837 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2838 {
2839 _bfd_error_handler
2840 /* xgettext:c-format */
2841 (_("%B: relocation R_386_GOTOFF against protected %s"
2842 " `%s' can not be used when making a shared object"),
2843 input_bfd,
2844 h->type == STT_FUNC ? "function" : "data",
2845 h->root.root.string);
2846 bfd_set_error (bfd_error_bad_value);
2847 return FALSE;
2848 }
2849 }
2850
2851 /* Note that sgot is not involved in this
2852 calculation. We always want the start of .got.plt. If we
2853 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2854 permitted by the ABI, we might have to change this
2855 calculation. */
2856 relocation -= htab->elf.sgotplt->output_section->vma
2857 + htab->elf.sgotplt->output_offset;
2858 break;
2859
2860 case R_386_GOTPC:
2861 /* Use global offset table as symbol value. */
2862 relocation = htab->elf.sgotplt->output_section->vma
2863 + htab->elf.sgotplt->output_offset;
2864 unresolved_reloc = FALSE;
2865 break;
2866
2867 case R_386_PLT32:
2868 /* Relocation is to the entry for this symbol in the
2869 procedure linkage table. */
2870
2871 /* Resolve a PLT32 reloc against a local symbol directly,
2872 without using the procedure linkage table. */
2873 if (h == NULL)
2874 break;
2875
2876 if ((h->plt.offset == (bfd_vma) -1
2877 && eh->plt_got.offset == (bfd_vma) -1)
2878 || htab->elf.splt == NULL)
2879 {
2880 /* We didn't make a PLT entry for this symbol. This
2881 happens when statically linking PIC code, or when
2882 using -Bsymbolic. */
2883 break;
2884 }
2885
2886 if (h->plt.offset != (bfd_vma) -1)
2887 {
2888 if (htab->plt_second != NULL)
2889 {
2890 resolved_plt = htab->plt_second;
2891 plt_offset = eh->plt_second.offset;
2892 }
2893 else
2894 {
2895 resolved_plt = htab->elf.splt;
2896 plt_offset = h->plt.offset;
2897 }
2898 }
2899 else
2900 {
2901 resolved_plt = htab->plt_got;
2902 plt_offset = eh->plt_got.offset;
2903 }
2904
2905 relocation = (resolved_plt->output_section->vma
2906 + resolved_plt->output_offset
2907 + plt_offset);
2908 unresolved_reloc = FALSE;
2909 break;
2910
2911 case R_386_SIZE32:
2912 /* Set to symbol size. */
2913 relocation = st_size;
2914 /* Fall through. */
2915
2916 case R_386_32:
2917 case R_386_PC32:
2918 if ((input_section->flags & SEC_ALLOC) == 0
2919 || is_vxworks_tls)
2920 break;
2921
2922 /* Copy dynamic function pointer relocations. Don't generate
2923 dynamic relocations against resolved undefined weak symbols
2924 in PIE, except for R_386_PC32. */
2925 if ((bfd_link_pic (info)
2926 && (h == NULL
2927 || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2928 && (!resolved_to_zero
2929 || r_type == R_386_PC32))
2930 || h->root.type != bfd_link_hash_undefweak))
2931 && ((r_type != R_386_PC32 && r_type != R_386_SIZE32)
2932 || !SYMBOL_CALLS_LOCAL (info, h)))
2933 || (ELIMINATE_COPY_RELOCS
2934 && !bfd_link_pic (info)
2935 && h != NULL
2936 && h->dynindx != -1
2937 && (!h->non_got_ref
2938 || eh->func_pointer_refcount > 0
2939 || (h->root.type == bfd_link_hash_undefweak
2940 && !resolved_to_zero))
2941 && ((h->def_dynamic && !h->def_regular)
2942 /* Undefined weak symbol is bound locally when
2943 PIC is false. */
2944 || h->root.type == bfd_link_hash_undefweak)))
2945 {
2946 Elf_Internal_Rela outrel;
2947 bfd_boolean skip, relocate;
2948 asection *sreloc;
2949
2950 /* When generating a shared object, these relocations
2951 are copied into the output file to be resolved at run
2952 time. */
2953
2954 skip = FALSE;
2955 relocate = FALSE;
2956
2957 outrel.r_offset =
2958 _bfd_elf_section_offset (output_bfd, info, input_section,
2959 rel->r_offset);
2960 if (outrel.r_offset == (bfd_vma) -1)
2961 skip = TRUE;
2962 else if (outrel.r_offset == (bfd_vma) -2)
2963 skip = TRUE, relocate = TRUE;
2964 outrel.r_offset += (input_section->output_section->vma
2965 + input_section->output_offset);
2966
2967 if (skip)
2968 memset (&outrel, 0, sizeof outrel);
2969 else if (h != NULL
2970 && h->dynindx != -1
2971 && (r_type == R_386_PC32
2972 || !(bfd_link_executable (info)
2973 || SYMBOLIC_BIND (info, h))
2974 || !h->def_regular))
2975 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2976 else
2977 {
2978 /* This symbol is local, or marked to become local. */
2979 relocate = TRUE;
2980 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2981 }
2982
2983 sreloc = elf_section_data (input_section)->sreloc;
2984
2985 if (sreloc == NULL || sreloc->contents == NULL)
2986 {
2987 r = bfd_reloc_notsupported;
2988 goto check_relocation_error;
2989 }
2990
2991 elf_append_rel (output_bfd, sreloc, &outrel);
2992
2993 /* If this reloc is against an external symbol, we do
2994 not want to fiddle with the addend. Otherwise, we
2995 need to include the symbol value so that it becomes
2996 an addend for the dynamic reloc. */
2997 if (! relocate)
2998 continue;
2999 }
3000 break;
3001
3002 case R_386_TLS_IE:
3003 if (!bfd_link_executable (info))
3004 {
3005 Elf_Internal_Rela outrel;
3006 asection *sreloc;
3007
3008 outrel.r_offset = rel->r_offset
3009 + input_section->output_section->vma
3010 + input_section->output_offset;
3011 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3012 sreloc = elf_section_data (input_section)->sreloc;
3013 if (sreloc == NULL)
3014 abort ();
3015 elf_append_rel (output_bfd, sreloc, &outrel);
3016 }
3017 /* Fall through */
3018
3019 case R_386_TLS_GD:
3020 case R_386_TLS_GOTDESC:
3021 case R_386_TLS_DESC_CALL:
3022 case R_386_TLS_IE_32:
3023 case R_386_TLS_GOTIE:
3024 tls_type = GOT_UNKNOWN;
3025 if (h == NULL && local_got_offsets)
3026 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
3027 else if (h != NULL)
3028 tls_type = elf_x86_hash_entry(h)->tls_type;
3029 if (tls_type == GOT_TLS_IE)
3030 tls_type = GOT_TLS_IE_NEG;
3031
3032 if (! elf_i386_tls_transition (info, input_bfd,
3033 input_section, contents,
3034 symtab_hdr, sym_hashes,
3035 &r_type, tls_type, rel,
3036 relend, h, r_symndx, TRUE))
3037 return FALSE;
3038
3039 if (r_type == R_386_TLS_LE_32)
3040 {
3041 BFD_ASSERT (! unresolved_reloc);
3042 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3043 {
3044 unsigned int type;
3045 bfd_vma roff;
3046
3047 /* GD->LE transition. */
3048 type = *(contents + rel->r_offset - 2);
3049 if (type == 0x04)
3050 {
3051 /* Change
3052 leal foo@tlsgd(,%ebx,1), %eax
3053 call ___tls_get_addr@PLT
3054 into:
3055 movl %gs:0, %eax
3056 subl $foo@tpoff, %eax
3057 (6 byte form of subl). */
3058 roff = rel->r_offset + 5;
3059 }
3060 else
3061 {
3062 /* Change
3063 leal foo@tlsgd(%ebx), %eax
3064 call ___tls_get_addr@PLT
3065 nop
3066 or
3067 leal foo@tlsgd(%reg), %eax
3068 call *___tls_get_addr@GOT(%reg)
3069 which may be converted to
3070 addr32 call ___tls_get_addr
3071 into:
3072 movl %gs:0, %eax; subl $foo@tpoff, %eax
3073 (6 byte form of subl). */
3074 roff = rel->r_offset + 6;
3075 }
3076 memcpy (contents + roff - 8,
3077 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3078 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3079 contents + roff);
3080 /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */
3081 rel++;
3082 wrel++;
3083 continue;
3084 }
3085 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3086 {
3087 /* GDesc -> LE transition.
3088 It's originally something like:
3089 leal x@tlsdesc(%ebx), %eax
3090
3091 leal x@ntpoff, %eax
3092
3093 Registers other than %eax may be set up here. */
3094
3095 unsigned int val;
3096 bfd_vma roff;
3097
3098 roff = rel->r_offset;
3099 val = bfd_get_8 (input_bfd, contents + roff - 1);
3100
3101 /* Now modify the instruction as appropriate. */
3102 /* aoliva FIXME: remove the above and xor the byte
3103 below with 0x86. */
3104 bfd_put_8 (output_bfd, val ^ 0x86,
3105 contents + roff - 1);
3106 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3107 contents + roff);
3108 continue;
3109 }
3110 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3111 {
3112 /* GDesc -> LE transition.
3113 It's originally:
3114 call *(%eax)
3115 Turn it into:
3116 xchg %ax,%ax */
3117
3118 bfd_vma roff;
3119
3120 roff = rel->r_offset;
3121 bfd_put_8 (output_bfd, 0x66, contents + roff);
3122 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3123 continue;
3124 }
3125 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
3126 {
3127 unsigned int val;
3128
3129 /* IE->LE transition:
3130 Originally it can be one of:
3131 movl foo, %eax
3132 movl foo, %reg
3133 addl foo, %reg
3134 We change it into:
3135 movl $foo, %eax
3136 movl $foo, %reg
3137 addl $foo, %reg. */
3138 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3139 if (val == 0xa1)
3140 {
3141 /* movl foo, %eax. */
3142 bfd_put_8 (output_bfd, 0xb8,
3143 contents + rel->r_offset - 1);
3144 }
3145 else
3146 {
3147 unsigned int type;
3148
3149 type = bfd_get_8 (input_bfd,
3150 contents + rel->r_offset - 2);
3151 switch (type)
3152 {
3153 case 0x8b:
3154 /* movl */
3155 bfd_put_8 (output_bfd, 0xc7,
3156 contents + rel->r_offset - 2);
3157 bfd_put_8 (output_bfd,
3158 0xc0 | ((val >> 3) & 7),
3159 contents + rel->r_offset - 1);
3160 break;
3161 case 0x03:
3162 /* addl */
3163 bfd_put_8 (output_bfd, 0x81,
3164 contents + rel->r_offset - 2);
3165 bfd_put_8 (output_bfd,
3166 0xc0 | ((val >> 3) & 7),
3167 contents + rel->r_offset - 1);
3168 break;
3169 default:
3170 BFD_FAIL ();
3171 break;
3172 }
3173 }
3174 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3175 contents + rel->r_offset);
3176 continue;
3177 }
3178 else
3179 {
3180 unsigned int val, type;
3181
3182 /* {IE_32,GOTIE}->LE transition:
3183 Originally it can be one of:
3184 subl foo(%reg1), %reg2
3185 movl foo(%reg1), %reg2
3186 addl foo(%reg1), %reg2
3187 We change it into:
3188 subl $foo, %reg2
3189 movl $foo, %reg2 (6 byte form)
3190 addl $foo, %reg2. */
3191 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3192 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3193 if (type == 0x8b)
3194 {
3195 /* movl */
3196 bfd_put_8 (output_bfd, 0xc7,
3197 contents + rel->r_offset - 2);
3198 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3199 contents + rel->r_offset - 1);
3200 }
3201 else if (type == 0x2b)
3202 {
3203 /* subl */
3204 bfd_put_8 (output_bfd, 0x81,
3205 contents + rel->r_offset - 2);
3206 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3207 contents + rel->r_offset - 1);
3208 }
3209 else if (type == 0x03)
3210 {
3211 /* addl */
3212 bfd_put_8 (output_bfd, 0x81,
3213 contents + rel->r_offset - 2);
3214 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3215 contents + rel->r_offset - 1);
3216 }
3217 else
3218 BFD_FAIL ();
3219 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
3220 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3221 contents + rel->r_offset);
3222 else
3223 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3224 contents + rel->r_offset);
3225 continue;
3226 }
3227 }
3228
3229 if (htab->elf.sgot == NULL)
3230 abort ();
3231
3232 if (h != NULL)
3233 {
3234 off = h->got.offset;
3235 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3236 }
3237 else
3238 {
3239 if (local_got_offsets == NULL)
3240 abort ();
3241
3242 off = local_got_offsets[r_symndx];
3243 offplt = local_tlsdesc_gotents[r_symndx];
3244 }
3245
3246 if ((off & 1) != 0)
3247 off &= ~1;
3248 else
3249 {
3250 Elf_Internal_Rela outrel;
3251 int dr_type;
3252 asection *sreloc;
3253
3254 if (htab->elf.srelgot == NULL)
3255 abort ();
3256
3257 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3258
3259 if (GOT_TLS_GDESC_P (tls_type))
3260 {
3261 bfd_byte *loc;
3262 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3263 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3264 <= htab->elf.sgotplt->size);
3265 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3266 + htab->elf.sgotplt->output_offset
3267 + offplt
3268 + htab->sgotplt_jump_table_size);
3269 sreloc = htab->elf.srelplt;
3270 loc = sreloc->contents;
3271 loc += (htab->next_tls_desc_index++
3272 * sizeof (Elf32_External_Rel));
3273 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3274 <= sreloc->contents + sreloc->size);
3275 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3276 if (indx == 0)
3277 {
3278 BFD_ASSERT (! unresolved_reloc);
3279 bfd_put_32 (output_bfd,
3280 relocation - _bfd_x86_elf_dtpoff_base (info),
3281 htab->elf.sgotplt->contents + offplt
3282 + htab->sgotplt_jump_table_size + 4);
3283 }
3284 else
3285 {
3286 bfd_put_32 (output_bfd, 0,
3287 htab->elf.sgotplt->contents + offplt
3288 + htab->sgotplt_jump_table_size + 4);
3289 }
3290 }
3291
3292 sreloc = htab->elf.srelgot;
3293
3294 outrel.r_offset = (htab->elf.sgot->output_section->vma
3295 + htab->elf.sgot->output_offset + off);
3296
3297 if (GOT_TLS_GD_P (tls_type))
3298 dr_type = R_386_TLS_DTPMOD32;
3299 else if (GOT_TLS_GDESC_P (tls_type))
3300 goto dr_done;
3301 else if (tls_type == GOT_TLS_IE_POS)
3302 dr_type = R_386_TLS_TPOFF;
3303 else
3304 dr_type = R_386_TLS_TPOFF32;
3305
3306 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3307 bfd_put_32 (output_bfd,
3308 relocation - _bfd_x86_elf_dtpoff_base (info),
3309 htab->elf.sgot->contents + off);
3310 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3311 bfd_put_32 (output_bfd,
3312 _bfd_x86_elf_dtpoff_base (info) - relocation,
3313 htab->elf.sgot->contents + off);
3314 else if (dr_type != R_386_TLS_DESC)
3315 bfd_put_32 (output_bfd, 0,
3316 htab->elf.sgot->contents + off);
3317 outrel.r_info = ELF32_R_INFO (indx, dr_type);
3318
3319 elf_append_rel (output_bfd, sreloc, &outrel);
3320
3321 if (GOT_TLS_GD_P (tls_type))
3322 {
3323 if (indx == 0)
3324 {
3325 BFD_ASSERT (! unresolved_reloc);
3326 bfd_put_32 (output_bfd,
3327 relocation - _bfd_x86_elf_dtpoff_base (info),
3328 htab->elf.sgot->contents + off + 4);
3329 }
3330 else
3331 {
3332 bfd_put_32 (output_bfd, 0,
3333 htab->elf.sgot->contents + off + 4);
3334 outrel.r_info = ELF32_R_INFO (indx,
3335 R_386_TLS_DTPOFF32);
3336 outrel.r_offset += 4;
3337 elf_append_rel (output_bfd, sreloc, &outrel);
3338 }
3339 }
3340 else if (tls_type == GOT_TLS_IE_BOTH)
3341 {
3342 bfd_put_32 (output_bfd,
3343 (indx == 0
3344 ? relocation - _bfd_x86_elf_dtpoff_base (info)
3345 : 0),
3346 htab->elf.sgot->contents + off + 4);
3347 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3348 outrel.r_offset += 4;
3349 elf_append_rel (output_bfd, sreloc, &outrel);
3350 }
3351
3352 dr_done:
3353 if (h != NULL)
3354 h->got.offset |= 1;
3355 else
3356 local_got_offsets[r_symndx] |= 1;
3357 }
3358
3359 if (off >= (bfd_vma) -2
3360 && ! GOT_TLS_GDESC_P (tls_type))
3361 abort ();
3362 if (r_type == R_386_TLS_GOTDESC
3363 || r_type == R_386_TLS_DESC_CALL)
3364 {
3365 relocation = htab->sgotplt_jump_table_size + offplt;
3366 unresolved_reloc = FALSE;
3367 }
3368 else if (r_type == ELF32_R_TYPE (rel->r_info))
3369 {
3370 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3371 + htab->elf.sgotplt->output_offset;
3372 relocation = htab->elf.sgot->output_section->vma
3373 + htab->elf.sgot->output_offset + off - g_o_t;
3374 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3375 && tls_type == GOT_TLS_IE_BOTH)
3376 relocation += 4;
3377 if (r_type == R_386_TLS_IE)
3378 relocation += g_o_t;
3379 unresolved_reloc = FALSE;
3380 }
3381 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3382 {
3383 unsigned int val, type;
3384 bfd_vma roff;
3385
3386 /* GD->IE transition. */
3387 type = *(contents + rel->r_offset - 2);
3388 val = *(contents + rel->r_offset - 1);
3389 if (type == 0x04)
3390 {
3391 /* Change
3392 leal foo@tlsgd(,%ebx,1), %eax
3393 call ___tls_get_addr@PLT
3394 into:
3395 movl %gs:0, %eax
3396 subl $foo@gottpoff(%ebx), %eax. */
3397 val >>= 3;
3398 roff = rel->r_offset - 3;
3399 }
3400 else
3401 {
3402 /* Change
3403 leal foo@tlsgd(%ebx), %eax
3404 call ___tls_get_addr@PLT
3405 nop
3406 or
3407 leal foo@tlsgd(%reg), %eax
3408 call *___tls_get_addr@GOT(%reg)
3409 which may be converted to
3410 addr32 call ___tls_get_addr
3411 into:
3412 movl %gs:0, %eax;
3413 subl $foo@gottpoff(%reg), %eax. */
3414 roff = rel->r_offset - 2;
3415 }
3416 memcpy (contents + roff,
3417 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3418 contents[roff + 7] = 0x80 | (val & 7);
3419 /* If foo is used only with foo@gotntpoff(%reg) and
3420 foo@indntpoff, but not with foo@gottpoff(%reg), change
3421 subl $foo@gottpoff(%reg), %eax
3422 into:
3423 addl $foo@gotntpoff(%reg), %eax. */
3424 if (tls_type == GOT_TLS_IE_POS)
3425 contents[roff + 6] = 0x03;
3426 bfd_put_32 (output_bfd,
3427 htab->elf.sgot->output_section->vma
3428 + htab->elf.sgot->output_offset + off
3429 - htab->elf.sgotplt->output_section->vma
3430 - htab->elf.sgotplt->output_offset,
3431 contents + roff + 8);
3432 /* Skip R_386_PLT32 and R_386_GOT32X. */
3433 rel++;
3434 wrel++;
3435 continue;
3436 }
3437 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3438 {
3439 /* GDesc -> IE transition.
3440 It's originally something like:
3441 leal x@tlsdesc(%ebx), %eax
3442
3443 Change it to:
3444 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3445 or:
3446 movl x@gottpoff(%ebx), %eax # before negl %eax
3447
3448 Registers other than %eax may be set up here. */
3449
3450 bfd_vma roff;
3451
3452 /* First, make sure it's a leal adding ebx to a 32-bit
3453 offset into any register, although it's probably
3454 almost always going to be eax. */
3455 roff = rel->r_offset;
3456
3457 /* Now modify the instruction as appropriate. */
3458 /* To turn a leal into a movl in the form we use it, it
3459 suffices to change the first byte from 0x8d to 0x8b.
3460 aoliva FIXME: should we decide to keep the leal, all
3461 we have to do is remove the statement below, and
3462 adjust the relaxation of R_386_TLS_DESC_CALL. */
3463 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3464
3465 if (tls_type == GOT_TLS_IE_BOTH)
3466 off += 4;
3467
3468 bfd_put_32 (output_bfd,
3469 htab->elf.sgot->output_section->vma
3470 + htab->elf.sgot->output_offset + off
3471 - htab->elf.sgotplt->output_section->vma
3472 - htab->elf.sgotplt->output_offset,
3473 contents + roff);
3474 continue;
3475 }
3476 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3477 {
3478 /* GDesc -> IE transition.
3479 It's originally:
3480 call *(%eax)
3481
3482 Change it to:
3483 xchg %ax,%ax
3484 or
3485 negl %eax
3486 depending on how we transformed the TLS_GOTDESC above.
3487 */
3488
3489 bfd_vma roff;
3490
3491 roff = rel->r_offset;
3492
3493 /* Now modify the instruction as appropriate. */
3494 if (tls_type != GOT_TLS_IE_NEG)
3495 {
3496 /* xchg %ax,%ax */
3497 bfd_put_8 (output_bfd, 0x66, contents + roff);
3498 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3499 }
3500 else
3501 {
3502 /* negl %eax */
3503 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3504 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3505 }
3506
3507 continue;
3508 }
3509 else
3510 BFD_ASSERT (FALSE);
3511 break;
3512
3513 case R_386_TLS_LDM:
3514 if (! elf_i386_tls_transition (info, input_bfd,
3515 input_section, contents,
3516 symtab_hdr, sym_hashes,
3517 &r_type, GOT_UNKNOWN, rel,
3518 relend, h, r_symndx, TRUE))
3519 return FALSE;
3520
3521 if (r_type != R_386_TLS_LDM)
3522 {
3523 /* LD->LE transition. Change
3524 leal foo@tlsldm(%ebx) %eax
3525 call ___tls_get_addr@PLT
3526 into:
3527 movl %gs:0, %eax
3528 nop
3529 leal 0(%esi,1), %esi
3530 or change
3531 leal foo@tlsldm(%reg) %eax
3532 call *___tls_get_addr@GOT(%reg)
3533 which may be converted to
3534 addr32 call ___tls_get_addr
3535 into:
3536 movl %gs:0, %eax
3537 leal 0(%esi), %esi */
3538 BFD_ASSERT (r_type == R_386_TLS_LE_32);
3539 if (*(contents + rel->r_offset + 4) == 0xff
3540 || *(contents + rel->r_offset + 4) == 0x67)
3541 memcpy (contents + rel->r_offset - 2,
3542 "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3543 else
3544 memcpy (contents + rel->r_offset - 2,
3545 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3546 /* Skip R_386_PC32/R_386_PLT32. */
3547 rel++;
3548 wrel++;
3549 continue;
3550 }
3551
3552 if (htab->elf.sgot == NULL)
3553 abort ();
3554
3555 off = htab->tls_ld_or_ldm_got.offset;
3556 if (off & 1)
3557 off &= ~1;
3558 else
3559 {
3560 Elf_Internal_Rela outrel;
3561
3562 if (htab->elf.srelgot == NULL)
3563 abort ();
3564
3565 outrel.r_offset = (htab->elf.sgot->output_section->vma
3566 + htab->elf.sgot->output_offset + off);
3567
3568 bfd_put_32 (output_bfd, 0,
3569 htab->elf.sgot->contents + off);
3570 bfd_put_32 (output_bfd, 0,
3571 htab->elf.sgot->contents + off + 4);
3572 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3573 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
3574 htab->tls_ld_or_ldm_got.offset |= 1;
3575 }
3576 relocation = htab->elf.sgot->output_section->vma
3577 + htab->elf.sgot->output_offset + off
3578 - htab->elf.sgotplt->output_section->vma
3579 - htab->elf.sgotplt->output_offset;
3580 unresolved_reloc = FALSE;
3581 break;
3582
3583 case R_386_TLS_LDO_32:
3584 if (!bfd_link_executable (info)
3585 || (input_section->flags & SEC_CODE) == 0)
3586 relocation -= _bfd_x86_elf_dtpoff_base (info);
3587 else
3588 /* When converting LDO to LE, we must negate. */
3589 relocation = -elf_i386_tpoff (info, relocation);
3590 break;
3591
3592 case R_386_TLS_LE_32:
3593 case R_386_TLS_LE:
3594 if (!bfd_link_executable (info))
3595 {
3596 Elf_Internal_Rela outrel;
3597 asection *sreloc;
3598
3599 outrel.r_offset = rel->r_offset
3600 + input_section->output_section->vma
3601 + input_section->output_offset;
3602 if (h != NULL && h->dynindx != -1)
3603 indx = h->dynindx;
3604 else
3605 indx = 0;
3606 if (r_type == R_386_TLS_LE_32)
3607 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3608 else
3609 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3610 sreloc = elf_section_data (input_section)->sreloc;
3611 if (sreloc == NULL)
3612 abort ();
3613 elf_append_rel (output_bfd, sreloc, &outrel);
3614 if (indx)
3615 continue;
3616 else if (r_type == R_386_TLS_LE_32)
3617 relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
3618 else
3619 relocation -= _bfd_x86_elf_dtpoff_base (info);
3620 }
3621 else if (r_type == R_386_TLS_LE_32)
3622 relocation = elf_i386_tpoff (info, relocation);
3623 else
3624 relocation = -elf_i386_tpoff (info, relocation);
3625 break;
3626
3627 default:
3628 break;
3629 }
3630
3631 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3632 because such sections are not SEC_ALLOC and thus ld.so will
3633 not process them. */
3634 if (unresolved_reloc
3635 && !((input_section->flags & SEC_DEBUGGING) != 0
3636 && h->def_dynamic)
3637 && _bfd_elf_section_offset (output_bfd, info, input_section,
3638 rel->r_offset) != (bfd_vma) -1)
3639 {
3640 _bfd_error_handler
3641 /* xgettext:c-format */
3642 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
3643 input_bfd,
3644 input_section,
3645 rel->r_offset,
3646 howto->name,
3647 h->root.root.string);
3648 return FALSE;
3649 }
3650
3651 do_relocation:
3652 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3653 contents, rel->r_offset,
3654 relocation, 0);
3655
3656 check_relocation_error:
3657 if (r != bfd_reloc_ok)
3658 {
3659 const char *name;
3660
3661 if (h != NULL)
3662 name = h->root.root.string;
3663 else
3664 {
3665 name = bfd_elf_string_from_elf_section (input_bfd,
3666 symtab_hdr->sh_link,
3667 sym->st_name);
3668 if (name == NULL)
3669 return FALSE;
3670 if (*name == '\0')
3671 name = bfd_section_name (input_bfd, sec);
3672 }
3673
3674 if (r == bfd_reloc_overflow)
3675 (*info->callbacks->reloc_overflow)
3676 (info, (h ? &h->root : NULL), name, howto->name,
3677 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3678 else
3679 {
3680 _bfd_error_handler
3681 /* xgettext:c-format */
3682 (_("%B(%A+%#Lx): reloc against `%s': error %d"),
3683 input_bfd, input_section,
3684 rel->r_offset, name, (int) r);
3685 return FALSE;
3686 }
3687 }
3688
3689 if (wrel != rel)
3690 *wrel = *rel;
3691 }
3692
3693 if (wrel != rel)
3694 {
3695 Elf_Internal_Shdr *rel_hdr;
3696 size_t deleted = rel - wrel;
3697
3698 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3699 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3700 if (rel_hdr->sh_size == 0)
3701 {
3702 /* It is too late to remove an empty reloc section. Leave
3703 one NONE reloc.
3704 ??? What is wrong with an empty section??? */
3705 rel_hdr->sh_size = rel_hdr->sh_entsize;
3706 deleted -= 1;
3707 }
3708 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3709 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3710 input_section->reloc_count -= deleted;
3711 }
3712
3713 return TRUE;
3714 }
3715
3716 /* Finish up dynamic symbol handling. We set the contents of various
3717 dynamic sections here. */
3718
3719 static bfd_boolean
3720 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3721 struct bfd_link_info *info,
3722 struct elf_link_hash_entry *h,
3723 Elf_Internal_Sym *sym)
3724 {
3725 struct elf_x86_link_hash_table *htab;
3726 unsigned plt_entry_size;
3727 struct elf_x86_link_hash_entry *eh;
3728 bfd_boolean local_undefweak;
3729 bfd_boolean use_plt_second;
3730
3731 htab = elf_x86_hash_table (info, I386_ELF_DATA);
3732 if (htab == NULL)
3733 return FALSE;
3734
3735 plt_entry_size = htab->plt.plt_entry_size;
3736
3737 /* Use the second PLT section only if there is .plt section. */
3738 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3739
3740 eh = (struct elf_x86_link_hash_entry *) h;
3741 if (eh->no_finish_dynamic_symbol)
3742 abort ();
3743
3744 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3745 resolved undefined weak symbols in executable so that their
3746 references have value 0 at run-time. */
3747 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, I386_ELF_DATA,
3748 eh->has_got_reloc,
3749 eh);
3750
3751 if (h->plt.offset != (bfd_vma) -1)
3752 {
3753 bfd_vma plt_index, plt_offset;
3754 bfd_vma got_offset;
3755 Elf_Internal_Rela rel;
3756 bfd_byte *loc;
3757 asection *plt, *resolved_plt, *gotplt, *relplt;
3758
3759 /* When building a static executable, use .iplt, .igot.plt and
3760 .rel.iplt sections for STT_GNU_IFUNC symbols. */
3761 if (htab->elf.splt != NULL)
3762 {
3763 plt = htab->elf.splt;
3764 gotplt = htab->elf.sgotplt;
3765 relplt = htab->elf.srelplt;
3766 }
3767 else
3768 {
3769 plt = htab->elf.iplt;
3770 gotplt = htab->elf.igotplt;
3771 relplt = htab->elf.irelplt;
3772 }
3773
3774 /* This symbol has an entry in the procedure linkage table. Set
3775 it up. */
3776
3777 if ((h->dynindx == -1
3778 && !local_undefweak
3779 && !((h->forced_local || bfd_link_executable (info))
3780 && h->def_regular
3781 && h->type == STT_GNU_IFUNC))
3782 || plt == NULL
3783 || gotplt == NULL
3784 || relplt == NULL)
3785 abort ();
3786
3787 /* Get the index in the procedure linkage table which
3788 corresponds to this symbol. This is the index of this symbol
3789 in all the symbols for which we are making plt entries. The
3790 first entry in the procedure linkage table is reserved.
3791
3792 Get the offset into the .got table of the entry that
3793 corresponds to this function. Each .got entry is 4 bytes.
3794 The first three are reserved.
3795
3796 For static executables, we don't reserve anything. */
3797
3798 if (plt == htab->elf.splt)
3799 {
3800 got_offset = (h->plt.offset / plt_entry_size
3801 - htab->plt.has_plt0);
3802 got_offset = (got_offset + 3) * 4;
3803 }
3804 else
3805 {
3806 got_offset = h->plt.offset / plt_entry_size;
3807 got_offset = got_offset * 4;
3808 }
3809
3810 /* Fill in the entry in the procedure linkage table and update
3811 the first slot. */
3812 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
3813 plt_entry_size);
3814
3815 if (use_plt_second)
3816 {
3817 const bfd_byte *plt_entry;
3818 if (bfd_link_pic (info))
3819 plt_entry = htab->non_lazy_plt->pic_plt_entry;
3820 else
3821 plt_entry = htab->non_lazy_plt->plt_entry;
3822 memcpy (htab->plt_second->contents + eh->plt_second.offset,
3823 plt_entry, htab->non_lazy_plt->plt_entry_size);
3824
3825 resolved_plt = htab->plt_second;
3826 plt_offset = eh->plt_second.offset;
3827 }
3828 else
3829 {
3830 resolved_plt = plt;
3831 plt_offset = h->plt.offset;
3832 }
3833
3834 if (! bfd_link_pic (info))
3835 {
3836 bfd_put_32 (output_bfd,
3837 (gotplt->output_section->vma
3838 + gotplt->output_offset
3839 + got_offset),
3840 resolved_plt->contents + plt_offset
3841 + htab->plt.plt_got_offset);
3842
3843 if (htab->is_vxworks)
3844 {
3845 int s, k, reloc_index;
3846
3847 /* Create the R_386_32 relocation referencing the GOT
3848 for this PLT entry. */
3849
3850 /* S: Current slot number (zero-based). */
3851 s = ((h->plt.offset - htab->plt.plt_entry_size)
3852 / htab->plt.plt_entry_size);
3853 /* K: Number of relocations for PLTResolve. */
3854 if (bfd_link_pic (info))
3855 k = PLTRESOLVE_RELOCS_SHLIB;
3856 else
3857 k = PLTRESOLVE_RELOCS;
3858 /* Skip the PLTresolve relocations, and the relocations for
3859 the other PLT slots. */
3860 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3861 loc = (htab->srelplt2->contents + reloc_index
3862 * sizeof (Elf32_External_Rel));
3863
3864 rel.r_offset = (plt->output_section->vma
3865 + plt->output_offset
3866 + h->plt.offset + 2),
3867 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3868 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3869
3870 /* Create the R_386_32 relocation referencing the beginning of
3871 the PLT for this GOT entry. */
3872 rel.r_offset = (htab->elf.sgotplt->output_section->vma
3873 + htab->elf.sgotplt->output_offset
3874 + got_offset);
3875 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3876 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3877 loc + sizeof (Elf32_External_Rel));
3878 }
3879 }
3880 else
3881 {
3882 bfd_put_32 (output_bfd, got_offset,
3883 resolved_plt->contents + plt_offset
3884 + htab->plt.plt_got_offset);
3885 }
3886
3887 /* Fill in the entry in the global offset table. Leave the entry
3888 as zero for undefined weak symbol in PIE. No PLT relocation
3889 against undefined weak symbol in PIE. */
3890 if (!local_undefweak)
3891 {
3892 if (htab->plt.has_plt0)
3893 bfd_put_32 (output_bfd,
3894 (plt->output_section->vma
3895 + plt->output_offset
3896 + h->plt.offset
3897 + htab->lazy_plt->plt_lazy_offset),
3898 gotplt->contents + got_offset);
3899
3900 /* Fill in the entry in the .rel.plt section. */
3901 rel.r_offset = (gotplt->output_section->vma
3902 + gotplt->output_offset
3903 + got_offset);
3904 if (h->dynindx == -1
3905 || ((bfd_link_executable (info)
3906 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3907 && h->def_regular
3908 && h->type == STT_GNU_IFUNC))
3909 {
3910 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
3911 h->root.root.string,
3912 h->root.u.def.section->owner);
3913
3914 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3915 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
3916 in the .got.plt section. */
3917 bfd_put_32 (output_bfd,
3918 (h->root.u.def.value
3919 + h->root.u.def.section->output_section->vma
3920 + h->root.u.def.section->output_offset),
3921 gotplt->contents + got_offset);
3922 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3923 /* R_386_IRELATIVE comes last. */
3924 plt_index = htab->next_irelative_index--;
3925 }
3926 else
3927 {
3928 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3929 plt_index = htab->next_jump_slot_index++;
3930 }
3931
3932 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3933 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3934
3935 /* Don't fill the second and third slots in PLT entry for
3936 static executables nor without PLT0. */
3937 if (plt == htab->elf.splt && htab->plt.has_plt0)
3938 {
3939 bfd_put_32 (output_bfd,
3940 plt_index * sizeof (Elf32_External_Rel),
3941 plt->contents + h->plt.offset
3942 + htab->lazy_plt->plt_reloc_offset);
3943 bfd_put_32 (output_bfd,
3944 - (h->plt.offset
3945 + htab->lazy_plt->plt_plt_offset + 4),
3946 (plt->contents + h->plt.offset
3947 + htab->lazy_plt->plt_plt_offset));
3948 }
3949 }
3950 }
3951 else if (eh->plt_got.offset != (bfd_vma) -1)
3952 {
3953 bfd_vma got_offset, plt_offset;
3954 asection *plt, *got, *gotplt;
3955 const bfd_byte *got_plt_entry;
3956
3957 /* Set the entry in the GOT procedure linkage table. */
3958 plt = htab->plt_got;
3959 got = htab->elf.sgot;
3960 gotplt = htab->elf.sgotplt;
3961 got_offset = h->got.offset;
3962
3963 if (got_offset == (bfd_vma) -1
3964 || plt == NULL
3965 || got == NULL
3966 || gotplt == NULL)
3967 abort ();
3968
3969 /* Fill in the entry in the GOT procedure linkage table. */
3970 if (! bfd_link_pic (info))
3971 {
3972 got_plt_entry = htab->non_lazy_plt->plt_entry;
3973 got_offset += got->output_section->vma + got->output_offset;
3974 }
3975 else
3976 {
3977 got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
3978 got_offset += (got->output_section->vma
3979 + got->output_offset
3980 - gotplt->output_section->vma
3981 - gotplt->output_offset);
3982 }
3983
3984 plt_offset = eh->plt_got.offset;
3985 memcpy (plt->contents + plt_offset, got_plt_entry,
3986 htab->non_lazy_plt->plt_entry_size);
3987 bfd_put_32 (output_bfd, got_offset,
3988 (plt->contents + plt_offset
3989 + htab->non_lazy_plt->plt_got_offset));
3990 }
3991
3992 if (!local_undefweak
3993 && !h->def_regular
3994 && (h->plt.offset != (bfd_vma) -1
3995 || eh->plt_got.offset != (bfd_vma) -1))
3996 {
3997 /* Mark the symbol as undefined, rather than as defined in
3998 the .plt section. Leave the value if there were any
3999 relocations where pointer equality matters (this is a clue
4000 for the dynamic linker, to make function pointer
4001 comparisons work between an application and shared
4002 library), otherwise set it to zero. If a function is only
4003 called from a binary, there is no need to slow down
4004 shared libraries because of that. */
4005 sym->st_shndx = SHN_UNDEF;
4006 if (!h->pointer_equality_needed)
4007 sym->st_value = 0;
4008 }
4009
4010 /* Don't generate dynamic GOT relocation against undefined weak
4011 symbol in executable. */
4012 if (h->got.offset != (bfd_vma) -1
4013 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
4014 && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
4015 && !local_undefweak)
4016 {
4017 Elf_Internal_Rela rel;
4018 asection *relgot = htab->elf.srelgot;
4019
4020 /* This symbol has an entry in the global offset table. Set it
4021 up. */
4022
4023 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4024 abort ();
4025
4026 rel.r_offset = (htab->elf.sgot->output_section->vma
4027 + htab->elf.sgot->output_offset
4028 + (h->got.offset & ~(bfd_vma) 1));
4029
4030 /* If this is a static link, or it is a -Bsymbolic link and the
4031 symbol is defined locally or was forced to be local because
4032 of a version file, we just want to emit a RELATIVE reloc.
4033 The entry in the global offset table will already have been
4034 initialized in the relocate_section function. */
4035 if (h->def_regular
4036 && h->type == STT_GNU_IFUNC)
4037 {
4038 if (h->plt.offset == (bfd_vma) -1)
4039 {
4040 /* STT_GNU_IFUNC is referenced without PLT. */
4041 if (htab->elf.splt == NULL)
4042 {
4043 /* use .rel[a].iplt section to store .got relocations
4044 in static executable. */
4045 relgot = htab->elf.irelplt;
4046 }
4047 if (SYMBOL_REFERENCES_LOCAL (info, h))
4048 {
4049 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
4050 h->root.root.string,
4051 h->root.u.def.section->owner);
4052
4053 bfd_put_32 (output_bfd,
4054 (h->root.u.def.value
4055 + h->root.u.def.section->output_section->vma
4056 + h->root.u.def.section->output_offset),
4057 htab->elf.sgot->contents + h->got.offset);
4058 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
4059 }
4060 else
4061 goto do_glob_dat;
4062 }
4063 else if (bfd_link_pic (info))
4064 {
4065 /* Generate R_386_GLOB_DAT. */
4066 goto do_glob_dat;
4067 }
4068 else
4069 {
4070 asection *plt;
4071 bfd_vma plt_offset;
4072
4073 if (!h->pointer_equality_needed)
4074 abort ();
4075
4076 /* For non-shared object, we can't use .got.plt, which
4077 contains the real function addres if we need pointer
4078 equality. We load the GOT entry with the PLT entry. */
4079 if (htab->plt_second != NULL)
4080 {
4081 plt = htab->plt_second;
4082 plt_offset = eh->plt_second.offset;
4083 }
4084 else
4085 {
4086 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4087 plt_offset = h->plt.offset;
4088 }
4089 bfd_put_32 (output_bfd,
4090 (plt->output_section->vma
4091 + plt->output_offset + plt_offset),
4092 htab->elf.sgot->contents + h->got.offset);
4093 return TRUE;
4094 }
4095 }
4096 else if (bfd_link_pic (info)
4097 && SYMBOL_REFERENCES_LOCAL (info, h))
4098 {
4099 BFD_ASSERT((h->got.offset & 1) != 0);
4100 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4101 }
4102 else
4103 {
4104 BFD_ASSERT((h->got.offset & 1) == 0);
4105 do_glob_dat:
4106 bfd_put_32 (output_bfd, (bfd_vma) 0,
4107 htab->elf.sgot->contents + h->got.offset);
4108 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
4109 }
4110
4111 elf_append_rel (output_bfd, relgot, &rel);
4112 }
4113
4114 if (h->needs_copy)
4115 {
4116 Elf_Internal_Rela rel;
4117 asection *s;
4118
4119 /* This symbol needs a copy reloc. Set it up. */
4120
4121 if (h->dynindx == -1
4122 || (h->root.type != bfd_link_hash_defined
4123 && h->root.type != bfd_link_hash_defweak)
4124 || htab->elf.srelbss == NULL
4125 || htab->elf.sreldynrelro == NULL)
4126 abort ();
4127
4128 rel.r_offset = (h->root.u.def.value
4129 + h->root.u.def.section->output_section->vma
4130 + h->root.u.def.section->output_offset);
4131 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
4132 if (h->root.u.def.section == htab->elf.sdynrelro)
4133 s = htab->elf.sreldynrelro;
4134 else
4135 s = htab->elf.srelbss;
4136 elf_append_rel (output_bfd, s, &rel);
4137 }
4138
4139 return TRUE;
4140 }
4141
4142 /* Finish up local dynamic symbol handling. We set the contents of
4143 various dynamic sections here. */
4144
4145 static bfd_boolean
4146 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
4147 {
4148 struct elf_link_hash_entry *h
4149 = (struct elf_link_hash_entry *) *slot;
4150 struct bfd_link_info *info
4151 = (struct bfd_link_info *) inf;
4152
4153 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
4154 h, NULL);
4155 }
4156
4157 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4158 here since undefined weak symbol may not be dynamic and may not be
4159 called for elf_i386_finish_dynamic_symbol. */
4160
4161 static bfd_boolean
4162 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4163 void *inf)
4164 {
4165 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4166 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4167
4168 if (h->root.type != bfd_link_hash_undefweak
4169 || h->dynindx != -1)
4170 return TRUE;
4171
4172 return elf_i386_finish_dynamic_symbol (info->output_bfd,
4173 info, h, NULL);
4174 }
4175
4176 /* Used to decide how to sort relocs in an optimal manner for the
4177 dynamic linker, before writing them out. */
4178
4179 static enum elf_reloc_type_class
4180 elf_i386_reloc_type_class (const struct bfd_link_info *info,
4181 const asection *rel_sec ATTRIBUTE_UNUSED,
4182 const Elf_Internal_Rela *rela)
4183 {
4184 bfd *abfd = info->output_bfd;
4185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4186 struct elf_link_hash_table *htab = elf_hash_table (info);
4187
4188 if (htab->dynsym != NULL
4189 && htab->dynsym->contents != NULL)
4190 {
4191 /* Check relocation against STT_GNU_IFUNC symbol if there are
4192 dynamic symbols. */
4193 unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
4194 if (r_symndx != STN_UNDEF)
4195 {
4196 Elf_Internal_Sym sym;
4197 if (!bed->s->swap_symbol_in (abfd,
4198 (htab->dynsym->contents
4199 + r_symndx * sizeof (Elf32_External_Sym)),
4200 0, &sym))
4201 abort ();
4202
4203 if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4204 return reloc_class_ifunc;
4205 }
4206 }
4207
4208 switch (ELF32_R_TYPE (rela->r_info))
4209 {
4210 case R_386_IRELATIVE:
4211 return reloc_class_ifunc;
4212 case R_386_RELATIVE:
4213 return reloc_class_relative;
4214 case R_386_JUMP_SLOT:
4215 return reloc_class_plt;
4216 case R_386_COPY:
4217 return reloc_class_copy;
4218 default:
4219 return reloc_class_normal;
4220 }
4221 }
4222
4223 /* Finish up the dynamic sections. */
4224
4225 static bfd_boolean
4226 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4227 struct bfd_link_info *info)
4228 {
4229 struct elf_x86_link_hash_table *htab;
4230 bfd *dynobj;
4231 asection *sdyn;
4232 const struct elf_i386_backend_data *abed;
4233
4234 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4235 if (htab == NULL)
4236 return FALSE;
4237
4238 dynobj = htab->elf.dynobj;
4239 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4240 abed = get_elf_i386_backend_data (output_bfd);
4241
4242 if (htab->elf.dynamic_sections_created)
4243 {
4244 Elf32_External_Dyn *dyncon, *dynconend;
4245
4246 if (sdyn == NULL || htab->elf.sgot == NULL)
4247 abort ();
4248
4249 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4250 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4251 for (; dyncon < dynconend; dyncon++)
4252 {
4253 Elf_Internal_Dyn dyn;
4254 asection *s;
4255
4256 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4257
4258 switch (dyn.d_tag)
4259 {
4260 default:
4261 if (htab->is_vxworks
4262 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
4263 break;
4264 continue;
4265
4266 case DT_PLTGOT:
4267 s = htab->elf.sgotplt;
4268 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4269 break;
4270
4271 case DT_JMPREL:
4272 s = htab->elf.srelplt;
4273 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4274 break;
4275
4276 case DT_PLTRELSZ:
4277 s = htab->elf.srelplt;
4278 dyn.d_un.d_val = s->size;
4279 break;
4280 }
4281
4282 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4283 }
4284
4285 if (htab->elf.splt && htab->elf.splt->size > 0)
4286 {
4287 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4288 really seem like the right value. */
4289 elf_section_data (htab->elf.splt->output_section)
4290 ->this_hdr.sh_entsize = 4;
4291
4292 if (htab->plt.has_plt0)
4293 {
4294 /* Fill in the special first entry in the procedure linkage
4295 table. */
4296 memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
4297 htab->lazy_plt->plt0_entry_size);
4298 memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
4299 abed->plt0_pad_byte,
4300 htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
4301 if (!bfd_link_pic (info))
4302 {
4303 bfd_put_32 (output_bfd,
4304 (htab->elf.sgotplt->output_section->vma
4305 + htab->elf.sgotplt->output_offset
4306 + 4),
4307 htab->elf.splt->contents
4308 + htab->lazy_plt->plt0_got1_offset);
4309 bfd_put_32 (output_bfd,
4310 (htab->elf.sgotplt->output_section->vma
4311 + htab->elf.sgotplt->output_offset
4312 + 8),
4313 htab->elf.splt->contents
4314 + htab->lazy_plt->plt0_got2_offset);
4315
4316 if (htab->is_vxworks)
4317 {
4318 Elf_Internal_Rela rel;
4319 int num_plts = (htab->elf.splt->size
4320 / htab->plt.plt_entry_size) - 1;
4321 unsigned char *p;
4322 asection *srelplt2 = htab->srelplt2;
4323
4324 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4325 + 4. On IA32 we use REL relocations so the
4326 addend goes in the PLT directly. */
4327 rel.r_offset = (htab->elf.splt->output_section->vma
4328 + htab->elf.splt->output_offset
4329 + htab->lazy_plt->plt0_got1_offset);
4330 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4331 R_386_32);
4332 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4333 srelplt2->contents);
4334 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4335 + 8. */
4336 rel.r_offset = (htab->elf.splt->output_section->vma
4337 + htab->elf.splt->output_offset
4338 + htab->lazy_plt->plt0_got2_offset);
4339 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4340 R_386_32);
4341 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4342 srelplt2->contents +
4343 sizeof (Elf32_External_Rel));
4344 /* Correct the .rel.plt.unloaded relocations. */
4345 p = srelplt2->contents;
4346 if (bfd_link_pic (info))
4347 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4348 else
4349 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4350
4351 for (; num_plts; num_plts--)
4352 {
4353 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4354 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4355 R_386_32);
4356 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4357 p += sizeof (Elf32_External_Rel);
4358
4359 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4360 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
4361 R_386_32);
4362 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4363 p += sizeof (Elf32_External_Rel);
4364 }
4365 }
4366 }
4367 }
4368 }
4369
4370 if (htab->plt_got != NULL && htab->plt_got->size > 0)
4371 elf_section_data (htab->plt_got->output_section)
4372 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
4373
4374 if (htab->plt_second != NULL && htab->plt_second->size > 0)
4375 elf_section_data (htab->plt_second->output_section)
4376 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
4377 }
4378
4379 /* Fill in the first three entries in the global offset table. */
4380 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
4381 {
4382 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4383 {
4384 _bfd_error_handler
4385 (_("discarded output section: `%A'"), htab->elf.sgotplt);
4386 return FALSE;
4387 }
4388
4389 bfd_put_32 (output_bfd,
4390 (sdyn == NULL ? 0
4391 : sdyn->output_section->vma + sdyn->output_offset),
4392 htab->elf.sgotplt->contents);
4393 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
4394 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
4395
4396 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
4397 }
4398
4399 /* Adjust .eh_frame for .plt section. */
4400 if (htab->plt_eh_frame != NULL
4401 && htab->plt_eh_frame->contents != NULL)
4402 {
4403 if (htab->elf.splt != NULL
4404 && htab->elf.splt->size != 0
4405 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4406 && htab->elf.splt->output_section != NULL
4407 && htab->plt_eh_frame->output_section != NULL)
4408 {
4409 bfd_vma plt_start = htab->elf.splt->output_section->vma;
4410 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
4411 + htab->plt_eh_frame->output_offset
4412 + PLT_FDE_START_OFFSET;
4413 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4414 htab->plt_eh_frame->contents
4415 + PLT_FDE_START_OFFSET);
4416 }
4417 if (htab->plt_eh_frame->sec_info_type
4418 == SEC_INFO_TYPE_EH_FRAME)
4419 {
4420 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4421 htab->plt_eh_frame,
4422 htab->plt_eh_frame->contents))
4423 return FALSE;
4424 }
4425 }
4426
4427 /* Adjust .eh_frame for .plt.got section. */
4428 if (htab->plt_got_eh_frame != NULL
4429 && htab->plt_got_eh_frame->contents != NULL)
4430 {
4431 if (htab->plt_got != NULL
4432 && htab->plt_got->size != 0
4433 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
4434 && htab->plt_got->output_section != NULL
4435 && htab->plt_got_eh_frame->output_section != NULL)
4436 {
4437 bfd_vma plt_start = htab->plt_got->output_section->vma;
4438 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
4439 + htab->plt_got_eh_frame->output_offset
4440 + PLT_FDE_START_OFFSET;
4441 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4442 htab->plt_got_eh_frame->contents
4443 + PLT_FDE_START_OFFSET);
4444 }
4445 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
4446 {
4447 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4448 htab->plt_got_eh_frame,
4449 htab->plt_got_eh_frame->contents))
4450 return FALSE;
4451 }
4452 }
4453
4454 /* Adjust .eh_frame for the second PLT section. */
4455 if (htab->plt_second_eh_frame != NULL
4456 && htab->plt_second_eh_frame->contents != NULL)
4457 {
4458 if (htab->plt_second != NULL
4459 && htab->plt_second->size != 0
4460 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
4461 && htab->plt_second->output_section != NULL
4462 && htab->plt_second_eh_frame->output_section != NULL)
4463 {
4464 bfd_vma plt_start = htab->plt_second->output_section->vma;
4465 bfd_vma eh_frame_start
4466 = (htab->plt_second_eh_frame->output_section->vma
4467 + htab->plt_second_eh_frame->output_offset
4468 + PLT_FDE_START_OFFSET);
4469 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4470 htab->plt_second_eh_frame->contents
4471 + PLT_FDE_START_OFFSET);
4472 }
4473 if (htab->plt_second_eh_frame->sec_info_type
4474 == SEC_INFO_TYPE_EH_FRAME)
4475 {
4476 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4477 htab->plt_second_eh_frame,
4478 htab->plt_second_eh_frame->contents))
4479 return FALSE;
4480 }
4481 }
4482
4483 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4484 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
4485
4486 /* Fill PLT entries for undefined weak symbols in PIE. */
4487 if (bfd_link_pie (info))
4488 bfd_hash_traverse (&info->hash->table,
4489 elf_i386_pie_finish_undefweak_symbol,
4490 info);
4491
4492 return TRUE;
4493 }
4494
4495 /* Fill PLT/GOT entries and allocate dynamic relocations for local
4496 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4497 It has to be done before elf_link_sort_relocs is called so that
4498 dynamic relocations are properly sorted. */
4499
4500 static bfd_boolean
4501 elf_i386_output_arch_local_syms
4502 (bfd *output_bfd ATTRIBUTE_UNUSED,
4503 struct bfd_link_info *info,
4504 void *flaginfo ATTRIBUTE_UNUSED,
4505 int (*func) (void *, const char *,
4506 Elf_Internal_Sym *,
4507 asection *,
4508 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4509 {
4510 struct elf_x86_link_hash_table *htab
4511 = elf_x86_hash_table (info, I386_ELF_DATA);
4512 if (htab == NULL)
4513 return FALSE;
4514
4515 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4516 htab_traverse (htab->loc_hash_table,
4517 elf_i386_finish_local_dynamic_symbol,
4518 info);
4519
4520 return TRUE;
4521 }
4522
4523 /* Forward declaration. */
4524 static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt;
4525
4526 /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4527 dynamic relocations. */
4528
4529 static long
4530 elf_i386_get_synthetic_symtab (bfd *abfd,
4531 long symcount ATTRIBUTE_UNUSED,
4532 asymbol **syms ATTRIBUTE_UNUSED,
4533 long dynsymcount,
4534 asymbol **dynsyms,
4535 asymbol **ret)
4536 {
4537 long count, i, n;
4538 int j;
4539 bfd_byte *plt_contents;
4540 long relsize;
4541 const struct elf_x86_lazy_plt_layout *lazy_plt;
4542 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4543 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4544 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4545 asection *plt;
4546 bfd_vma got_addr;
4547 enum elf_x86_plt_type plt_type;
4548 struct elf_x86_plt plts[] =
4549 {
4550 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4551 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4552 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4553 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4554 };
4555
4556 *ret = NULL;
4557
4558 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4559 return 0;
4560
4561 if (dynsymcount <= 0)
4562 return 0;
4563
4564 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4565 if (relsize <= 0)
4566 return -1;
4567
4568 non_lazy_plt = NULL;
4569 /* Silence GCC 6. */
4570 lazy_plt = NULL;
4571 non_lazy_ibt_plt = NULL;
4572 lazy_ibt_plt = NULL;
4573 switch (get_elf_i386_backend_data (abfd)->os)
4574 {
4575 case is_normal:
4576 non_lazy_plt = &elf_i386_non_lazy_plt;
4577 lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4578 non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4579 /* Fall through */
4580 case is_vxworks:
4581 lazy_plt = &elf_i386_lazy_plt;
4582 break;
4583 case is_nacl:
4584 lazy_plt = &elf_i386_nacl_plt;
4585 break;
4586 }
4587
4588 got_addr = 0;
4589
4590 count = 0;
4591 for (j = 0; plts[j].name != NULL; j++)
4592 {
4593 plt = bfd_get_section_by_name (abfd, plts[j].name);
4594 if (plt == NULL || plt->size == 0)
4595 continue;
4596
4597 /* Get the PLT section contents. */
4598 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4599 if (plt_contents == NULL)
4600 break;
4601 if (!bfd_get_section_contents (abfd, (asection *) plt,
4602 plt_contents, 0, plt->size))
4603 {
4604 free (plt_contents);
4605 break;
4606 }
4607
4608 /* Check what kind of PLT it is. */
4609 plt_type = plt_unknown;
4610 if (plts[j].type == plt_unknown
4611 && (plt->size >= (lazy_plt->plt0_entry_size
4612 + lazy_plt->plt_entry_size)))
4613 {
4614 /* Match lazy PLT first. */
4615 if (memcmp (plt_contents, lazy_plt->plt0_entry,
4616 lazy_plt->plt0_got1_offset) == 0)
4617 {
4618 /* The fist entry in the lazy IBT PLT is the same as the
4619 normal lazy PLT. */
4620 if (lazy_ibt_plt != NULL
4621 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4622 lazy_ibt_plt->plt_entry,
4623 lazy_ibt_plt->plt_got_offset) == 0))
4624 plt_type = plt_lazy | plt_second;
4625 else
4626 plt_type = plt_lazy;
4627 }
4628 else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4629 lazy_plt->plt0_got1_offset) == 0)
4630 {
4631 /* The fist entry in the PIC lazy IBT PLT is the same as
4632 the normal PIC lazy PLT. */
4633 if (lazy_ibt_plt != NULL
4634 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4635 lazy_ibt_plt->pic_plt_entry,
4636 lazy_ibt_plt->plt_got_offset) == 0))
4637 plt_type = plt_lazy | plt_pic | plt_second;
4638 else
4639 plt_type = plt_lazy | plt_pic;
4640 }
4641 }
4642
4643 if (non_lazy_plt != NULL
4644 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4645 && plt->size >= non_lazy_plt->plt_entry_size)
4646 {
4647 /* Match non-lazy PLT. */
4648 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4649 non_lazy_plt->plt_got_offset) == 0)
4650 plt_type = plt_non_lazy;
4651 else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4652 non_lazy_plt->plt_got_offset) == 0)
4653 plt_type = plt_pic;
4654 }
4655
4656 if ((non_lazy_ibt_plt != NULL)
4657 && (plt_type == plt_unknown || plt_type == plt_second)
4658 && plt->size >= non_lazy_ibt_plt->plt_entry_size)
4659 {
4660 if (memcmp (plt_contents,
4661 non_lazy_ibt_plt->plt_entry,
4662 non_lazy_ibt_plt->plt_got_offset) == 0)
4663 {
4664 /* Match IBT PLT. */
4665 plt_type = plt_second;
4666 non_lazy_plt = non_lazy_ibt_plt;
4667 }
4668 else if (memcmp (plt_contents,
4669 non_lazy_ibt_plt->pic_plt_entry,
4670 non_lazy_ibt_plt->plt_got_offset) == 0)
4671 {
4672 /* Match PIC IBT PLT. */
4673 plt_type = plt_second | plt_pic;
4674 non_lazy_plt = non_lazy_ibt_plt;
4675 }
4676 }
4677
4678 if (plt_type == plt_unknown)
4679 {
4680 free (plt_contents);
4681 continue;
4682 }
4683
4684 plts[j].sec = plt;
4685 plts[j].type = plt_type;
4686
4687 if ((plt_type & plt_lazy))
4688 {
4689 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4690 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4691 /* Skip PLT0 in lazy PLT. */
4692 i = 1;
4693 }
4694 else
4695 {
4696 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4697 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4698 i = 0;
4699 }
4700
4701 /* Skip lazy PLT when the second PLT is used. */
4702 if ((plt_type & (plt_lazy | plt_second))
4703 == (plt_lazy | plt_second))
4704 plts[j].count = 0;
4705 else
4706 {
4707 n = plt->size / plts[j].plt_entry_size;
4708 plts[j].count = n;
4709 count += n - i;
4710 }
4711
4712 plts[j].contents = plt_contents;
4713
4714 /* The _GLOBAL_OFFSET_TABLE_ address is needed. */
4715 if ((plt_type & plt_pic))
4716 got_addr = (bfd_vma) -1;
4717 }
4718
4719 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4720 got_addr, plts, dynsyms,
4721 ret);
4722 }
4723
4724 /* Set up i386 GNU properties. Return the first relocatable ELF input
4725 with GNU properties if found. Otherwise, return NULL. */
4726
4727 static bfd *
4728 elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4729 {
4730 struct elf_x86_plt_layout_table plt_layout;
4731
4732 plt_layout.normal_target = FALSE;
4733 plt_layout.is_vxworks = FALSE;
4734 switch (get_elf_i386_backend_data (info->output_bfd)->os)
4735 {
4736 case is_normal:
4737 plt_layout.lazy_plt = &elf_i386_lazy_plt;
4738 plt_layout.non_lazy_plt = &elf_i386_non_lazy_plt;
4739 plt_layout.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4740 plt_layout.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4741 plt_layout.normal_target = TRUE;
4742 break;
4743 case is_vxworks:
4744 plt_layout.lazy_plt = &elf_i386_lazy_plt;
4745 plt_layout.non_lazy_plt = NULL;
4746 plt_layout.lazy_ibt_plt = NULL;
4747 plt_layout.non_lazy_ibt_plt = NULL;
4748 plt_layout.is_vxworks = TRUE;
4749 break;
4750 case is_nacl:
4751 plt_layout.lazy_plt = &elf_i386_nacl_plt;
4752 plt_layout.non_lazy_plt = NULL;
4753 plt_layout.lazy_ibt_plt = NULL;
4754 plt_layout.non_lazy_ibt_plt = NULL;
4755 break;
4756 }
4757
4758 return _bfd_x86_elf_link_setup_gnu_properties (info, &plt_layout);
4759 }
4760
4761 #define TARGET_LITTLE_SYM i386_elf32_vec
4762 #define TARGET_LITTLE_NAME "elf32-i386"
4763 #define ELF_ARCH bfd_arch_i386
4764 #define ELF_TARGET_ID I386_ELF_DATA
4765 #define ELF_MACHINE_CODE EM_386
4766 #define ELF_MAXPAGESIZE 0x1000
4767
4768 #define elf_backend_can_gc_sections 1
4769 #define elf_backend_can_refcount 1
4770 #define elf_backend_want_got_plt 1
4771 #define elf_backend_plt_readonly 1
4772 #define elf_backend_want_plt_sym 0
4773 #define elf_backend_got_header_size 12
4774 #define elf_backend_plt_alignment 4
4775 #define elf_backend_dtrel_excludes_plt 1
4776 #define elf_backend_extern_protected_data 1
4777 #define elf_backend_caches_rawsize 1
4778 #define elf_backend_want_dynrelro 1
4779
4780 /* Support RELA for objdump of prelink objects. */
4781 #define elf_info_to_howto elf_i386_info_to_howto_rel
4782 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4783
4784 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4785 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
4786 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
4787 #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
4788
4789 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4790 #define elf_backend_check_relocs elf_i386_check_relocs
4791 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
4792 #define elf_backend_fake_sections elf_i386_fake_sections
4793 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4794 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4795 #define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms
4796 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
4797 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
4798 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
4799 #define elf_backend_relocate_section elf_i386_relocate_section
4800 #define elf_backend_setup_gnu_properties elf_i386_link_setup_gnu_properties
4801
4802 #include "elf32-target.h"
4803
4804 /* FreeBSD support. */
4805
4806 #undef TARGET_LITTLE_SYM
4807 #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
4808 #undef TARGET_LITTLE_NAME
4809 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
4810 #undef ELF_OSABI
4811 #define ELF_OSABI ELFOSABI_FREEBSD
4812
4813 /* The kernel recognizes executables as valid only if they carry a
4814 "FreeBSD" label in the ELF header. So we put this label on all
4815 executables and (for simplicity) also all other object files. */
4816
4817 static void
4818 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
4819 {
4820 _bfd_elf_post_process_headers (abfd, info);
4821
4822 #ifdef OLD_FREEBSD_ABI_LABEL
4823 {
4824 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4825 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4826 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4827 }
4828 #endif
4829 }
4830
4831 #undef elf_backend_post_process_headers
4832 #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
4833 #undef elf32_bed
4834 #define elf32_bed elf32_i386_fbsd_bed
4835
4836 #undef elf_backend_add_symbol_hook
4837
4838 #include "elf32-target.h"
4839
4840 /* Solaris 2. */
4841
4842 #undef TARGET_LITTLE_SYM
4843 #define TARGET_LITTLE_SYM i386_elf32_sol2_vec
4844 #undef TARGET_LITTLE_NAME
4845 #define TARGET_LITTLE_NAME "elf32-i386-sol2"
4846
4847 #undef elf_backend_post_process_headers
4848
4849 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4850 objects won't be recognized. */
4851 #undef ELF_OSABI
4852
4853 #undef elf32_bed
4854 #define elf32_bed elf32_i386_sol2_bed
4855
4856 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4857 boundary. */
4858 #undef elf_backend_static_tls_alignment
4859 #define elf_backend_static_tls_alignment 8
4860
4861 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4862
4863 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4864 File, p.63. */
4865 #undef elf_backend_want_plt_sym
4866 #define elf_backend_want_plt_sym 1
4867
4868 #undef elf_backend_strtab_flags
4869 #define elf_backend_strtab_flags SHF_STRINGS
4870
4871 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
4872 has a type >= SHT_LOOS. Returns TRUE if these fields were initialised
4873 FALSE otherwise. ISECTION is the best guess matching section from the
4874 input bfd IBFD, but it might be NULL. */
4875
4876 static bfd_boolean
4877 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
4878 bfd *obfd ATTRIBUTE_UNUSED,
4879 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
4880 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
4881 {
4882 /* PR 19938: FIXME: Need to add code for setting the sh_info
4883 and sh_link fields of Solaris specific section types. */
4884 return FALSE;
4885
4886 /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
4887 Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation:
4888
4889 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
4890
4891 The following values should be set:
4892
4893 Type Link Info
4894 -----------------------------------------------------------------------------
4895 SHT_SUNW_ancillary The section header index of 0
4896 [0x6fffffee] the associated string table.
4897
4898 SHT_SUNW_capinfo The section header index of For a dynamic object, the
4899 [0x6ffffff0] the associated symbol table. section header index of
4900 the associated
4901 SHT_SUNW_capchain table,
4902 otherwise 0.
4903
4904 SHT_SUNW_symsort The section header index of 0
4905 [0x6ffffff1] the associated symbol table.
4906
4907 SHT_SUNW_tlssort The section header index of 0
4908 [0x6ffffff2] the associated symbol table.
4909
4910 SHT_SUNW_LDYNSYM The section header index of One greater than the
4911 [0x6ffffff3] the associated string table. symbol table index of the
4912 This index is the same string last local symbol,
4913 table used by the SHT_DYNSYM STB_LOCAL. Since
4914 section. SHT_SUNW_LDYNSYM only
4915 contains local symbols,
4916 sh_info is equivalent to
4917 the number of symbols in
4918 the table.
4919
4920 SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer
4921 [0x6ffffff5] the section header index of to named strings, the
4922 the associated section header index of
4923 SHT_SUNW_capinfo table, the associated string
4924 otherwise 0. table, otherwise 0.
4925
4926 SHT_SUNW_move The section header index of 0
4927 [0x6ffffffa] the associated symbol table.
4928
4929 SHT_SUNW_COMDAT 0 0
4930 [0x6ffffffb]
4931
4932 SHT_SUNW_syminfo The section header index of The section header index
4933 [0x6ffffffc] the associated symbol table. of the associated
4934 .dynamic section.
4935
4936 SHT_SUNW_verdef The section header index of The number of version
4937 [0x6ffffffd] the associated string table. definitions within the
4938 section.
4939
4940 SHT_SUNW_verneed The section header index of The number of version
4941 [0x6ffffffe] the associated string table. dependencies within the
4942 section.
4943
4944 SHT_SUNW_versym The section header index of 0
4945 [0x6fffffff] the associated symbol table. */
4946 }
4947
4948 #undef elf_backend_copy_special_section_fields
4949 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
4950
4951 #include "elf32-target.h"
4952
4953 /* Intel MCU support. */
4954
4955 static bfd_boolean
4956 elf32_iamcu_elf_object_p (bfd *abfd)
4957 {
4958 /* Set the right machine number for an IAMCU elf32 file. */
4959 bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
4960 return TRUE;
4961 }
4962
4963 #undef TARGET_LITTLE_SYM
4964 #define TARGET_LITTLE_SYM iamcu_elf32_vec
4965 #undef TARGET_LITTLE_NAME
4966 #define TARGET_LITTLE_NAME "elf32-iamcu"
4967 #undef ELF_ARCH
4968 #define ELF_ARCH bfd_arch_iamcu
4969
4970 #undef ELF_MACHINE_CODE
4971 #define ELF_MACHINE_CODE EM_IAMCU
4972
4973 #undef ELF_OSABI
4974
4975 #undef elf32_bed
4976 #define elf32_bed elf32_iamcu_bed
4977
4978 #undef elf_backend_object_p
4979 #define elf_backend_object_p elf32_iamcu_elf_object_p
4980
4981 #undef elf_backend_static_tls_alignment
4982
4983 #undef elf_backend_want_plt_sym
4984 #define elf_backend_want_plt_sym 0
4985
4986 #undef elf_backend_strtab_flags
4987 #undef elf_backend_copy_special_section_fields
4988
4989 #include "elf32-target.h"
4990
4991 /* Restore defaults. */
4992 #undef ELF_ARCH
4993 #define ELF_ARCH bfd_arch_i386
4994 #undef ELF_MACHINE_CODE
4995 #define ELF_MACHINE_CODE EM_386
4996
4997 /* Native Client support. */
4998
4999 #undef TARGET_LITTLE_SYM
5000 #define TARGET_LITTLE_SYM i386_elf32_nacl_vec
5001 #undef TARGET_LITTLE_NAME
5002 #define TARGET_LITTLE_NAME "elf32-i386-nacl"
5003 #undef elf32_bed
5004 #define elf32_bed elf32_i386_nacl_bed
5005
5006 #undef ELF_MAXPAGESIZE
5007 #define ELF_MAXPAGESIZE 0x10000
5008
5009 /* Restore defaults. */
5010 #undef ELF_OSABI
5011 #undef elf_backend_want_plt_sym
5012 #define elf_backend_want_plt_sym 0
5013 #undef elf_backend_post_process_headers
5014 #undef elf_backend_static_tls_alignment
5015
5016 /* NaCl uses substantially different PLT entries for the same effects. */
5017
5018 #undef elf_backend_plt_alignment
5019 #define elf_backend_plt_alignment 5
5020 #define NACL_PLT_ENTRY_SIZE 64
5021 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
5022
5023 static const bfd_byte elf_i386_nacl_plt0_entry[] =
5024 {
5025 0xff, 0x35, /* pushl contents of address */
5026 0, 0, 0, 0, /* replaced with address of .got + 4. */
5027 0x8b, 0x0d, /* movl contents of address, %ecx */
5028 0, 0, 0, 0, /* replaced with address of .got + 8. */
5029 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
5030 0xff, 0xe1 /* jmp *%ecx */
5031 };
5032
5033 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
5034 {
5035 0x8b, 0x0d, /* movl contents of address, %ecx */
5036 0, 0, 0, 0, /* replaced with GOT slot address. */
5037 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
5038 0xff, 0xe1, /* jmp *%ecx */
5039
5040 /* Pad to the next 32-byte boundary with nop instructions. */
5041 0x90,
5042 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5043 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5044
5045 /* Lazy GOT entries point here (32-byte aligned). */
5046 0x68, /* pushl immediate */
5047 0, 0, 0, 0, /* replaced with reloc offset. */
5048 0xe9, /* jmp relative */
5049 0, 0, 0, 0, /* replaced with offset to .plt. */
5050
5051 /* Pad to the next 32-byte boundary with nop instructions. */
5052 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5053 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5054 0x90, 0x90
5055 };
5056
5057 static const bfd_byte
5058 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
5059 {
5060 0xff, 0x73, 0x04, /* pushl 4(%ebx) */
5061 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */
5062 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */
5063 0xff, 0xe1, /* jmp *%ecx */
5064
5065 /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
5066 so pad to that size with nop instructions. */
5067 0x90, 0x90, 0x90, 0x90, 0x90, 0x90
5068 };
5069
5070 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
5071 {
5072 0x8b, 0x8b, /* movl offset(%ebx), %ecx */
5073 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
5074 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */
5075 0xff, 0xe1, /* jmp *%ecx */
5076
5077 /* Pad to the next 32-byte boundary with nop instructions. */
5078 0x90,
5079 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5080 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5081
5082 /* Lazy GOT entries point here (32-byte aligned). */
5083 0x68, /* pushl immediate */
5084 0, 0, 0, 0, /* replaced with offset into relocation table. */
5085 0xe9, /* jmp relative */
5086 0, 0, 0, 0, /* replaced with offset to start of .plt. */
5087
5088 /* Pad to the next 32-byte boundary with nop instructions. */
5089 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5090 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5091 0x90, 0x90
5092 };
5093
5094 static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
5095 {
5096 #if (PLT_CIE_LENGTH != 20 \
5097 || PLT_FDE_LENGTH != 36 \
5098 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
5099 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
5100 # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
5101 #endif
5102 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
5103 0, 0, 0, 0, /* CIE ID */
5104 1, /* CIE version */
5105 'z', 'R', 0, /* Augmentation string */
5106 1, /* Code alignment factor */
5107 0x7c, /* Data alignment factor: -4 */
5108 8, /* Return address column */
5109 1, /* Augmentation size */
5110 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
5111 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
5112 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
5113 DW_CFA_nop, DW_CFA_nop,
5114
5115 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
5116 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
5117 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
5118 0, 0, 0, 0, /* .plt size goes here */
5119 0, /* Augmentation size */
5120 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
5121 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
5122 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
5123 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
5124 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
5125 13, /* Block length */
5126 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
5127 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
5128 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
5129 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
5130 DW_CFA_nop, DW_CFA_nop
5131 };
5132
5133 static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt =
5134 {
5135 elf_i386_nacl_plt0_entry, /* plt0_entry */
5136 sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */
5137 elf_i386_nacl_plt_entry, /* plt_entry */
5138 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
5139 2, /* plt0_got1_offset */
5140 8, /* plt0_got2_offset */
5141 0, /* plt0_got2_insn_end */
5142 2, /* plt_got_offset */
5143 33, /* plt_reloc_offset */
5144 38, /* plt_plt_offset */
5145 0, /* plt_got_insn_size */
5146 0, /* plt_plt_insn_end */
5147 32, /* plt_lazy_offset */
5148 elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */
5149 elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */
5150 elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */
5151 sizeof (elf_i386_nacl_eh_frame_plt) /* eh_frame_plt_size */
5152 };
5153
5154 static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
5155 {
5156 0x90, /* plt0_pad_byte: nop insn */
5157 is_nacl /* os */
5158 };
5159
5160 static bfd_boolean
5161 elf32_i386_nacl_elf_object_p (bfd *abfd)
5162 {
5163 /* Set the right machine number for a NaCl i386 ELF32 file. */
5164 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
5165 return TRUE;
5166 }
5167
5168 #undef elf_backend_arch_data
5169 #define elf_backend_arch_data &elf_i386_nacl_arch_bed
5170
5171 #undef elf_backend_object_p
5172 #define elf_backend_object_p elf32_i386_nacl_elf_object_p
5173 #undef elf_backend_modify_segment_map
5174 #define elf_backend_modify_segment_map nacl_modify_segment_map
5175 #undef elf_backend_modify_program_headers
5176 #define elf_backend_modify_program_headers nacl_modify_program_headers
5177 #undef elf_backend_final_write_processing
5178 #define elf_backend_final_write_processing nacl_final_write_processing
5179
5180 #include "elf32-target.h"
5181
5182 /* Restore defaults. */
5183 #undef elf_backend_object_p
5184 #undef elf_backend_modify_segment_map
5185 #undef elf_backend_modify_program_headers
5186 #undef elf_backend_final_write_processing
5187
5188 /* VxWorks support. */
5189
5190 #undef TARGET_LITTLE_SYM
5191 #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
5192 #undef TARGET_LITTLE_NAME
5193 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
5194 #undef ELF_OSABI
5195 #undef ELF_MAXPAGESIZE
5196 #define ELF_MAXPAGESIZE 0x1000
5197 #undef elf_backend_plt_alignment
5198 #define elf_backend_plt_alignment 4
5199
5200 static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
5201 {
5202 0x90, /* plt0_pad_byte */
5203 is_vxworks /* os */
5204 };
5205
5206 #undef elf_backend_arch_data
5207 #define elf_backend_arch_data &elf_i386_vxworks_arch_bed
5208
5209 #undef elf_backend_relocs_compatible
5210 #undef elf_backend_add_symbol_hook
5211 #define elf_backend_add_symbol_hook \
5212 elf_vxworks_add_symbol_hook
5213 #undef elf_backend_link_output_symbol_hook
5214 #define elf_backend_link_output_symbol_hook \
5215 elf_vxworks_link_output_symbol_hook
5216 #undef elf_backend_emit_relocs
5217 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
5218 #undef elf_backend_final_write_processing
5219 #define elf_backend_final_write_processing \
5220 elf_vxworks_final_write_processing
5221 #undef elf_backend_static_tls_alignment
5222
5223 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
5224 define it. */
5225 #undef elf_backend_want_plt_sym
5226 #define elf_backend_want_plt_sym 1
5227
5228 #undef elf32_bed
5229 #define elf32_bed elf32_i386_vxworks_bed
5230
5231 #include "elf32-target.h"