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