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