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