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