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