Mon Sep 25 22:49:32 1995 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
[deliverable/binutils-gdb.git] / bfd / elf32-i386.c
CommitLineData
e4b6b3e7 1/* Intel 80386/80486-specific support for 32-bit ELF
30dc85f1 2 Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
e4b6b3e7
ILT
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
943fbd5b 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
e4b6b3e7 19
e4b6b3e7
ILT
20#include "bfd.h"
21#include "sysdep.h"
013dec1a 22#include "bfdlink.h"
e4b6b3e7
ILT
23#include "libbfd.h"
24#include "libelf.h"
25
30dc85f1 26static reloc_howto_type *elf_i386_reloc_type_lookup
013dec1a
ILT
27 PARAMS ((bfd *, bfd_reloc_code_real_type));
28static void elf_i386_info_to_howto
29 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
30static void elf_i386_info_to_howto_rel
31 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
eb4267a3
ILT
32static boolean elf_i386_check_relocs
33 PARAMS ((bfd *, struct bfd_link_info *, asection *,
34 const Elf_Internal_Rela *));
013dec1a
ILT
35static boolean elf_i386_adjust_dynamic_symbol
36 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
013dec1a
ILT
37static boolean elf_i386_size_dynamic_sections
38 PARAMS ((bfd *, struct bfd_link_info *));
39static boolean elf_i386_relocate_section
40 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
eb4267a3 41 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
013dec1a
ILT
42static boolean elf_i386_finish_dynamic_symbol
43 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
44 Elf_Internal_Sym *));
45static boolean elf_i386_finish_dynamic_sections
46 PARAMS ((bfd *, struct bfd_link_info *));
47
e4b6b3e7
ILT
48#define USE_REL 1 /* 386 uses REL relocations instead of RELA */
49
50enum reloc_type
51 {
52 R_386_NONE = 0,
68241b2b
ILT
53 R_386_32,
54 R_386_PC32,
55 R_386_GOT32,
56 R_386_PLT32,
e4b6b3e7 57 R_386_COPY,
68241b2b
ILT
58 R_386_GLOB_DAT,
59 R_386_JUMP_SLOT,
e4b6b3e7 60 R_386_RELATIVE,
68241b2b
ILT
61 R_386_GOTOFF,
62 R_386_GOTPC,
e4b6b3e7
ILT
63 R_386_max
64 };
65
66#if 0
67static CONST char *CONST reloc_type_names[] =
68{
69 "R_386_NONE",
68241b2b
ILT
70 "R_386_32",
71 "R_386_PC32",
72 "R_386_GOT32",
73 "R_386_PLT32",
e4b6b3e7 74 "R_386_COPY",
68241b2b
ILT
75 "R_386_GLOB_DAT",
76 "R_386_JUMP_SLOT",
e4b6b3e7 77 "R_386_RELATIVE",
68241b2b
ILT
78 "R_386_GOTOFF",
79 "R_386_GOTPC",
e4b6b3e7
ILT
80};
81#endif
82
e4b6b3e7
ILT
83static reloc_howto_type elf_howto_table[]=
84{
68241b2b
ILT
85 HOWTO(R_386_NONE, 0,0, 0,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_NONE", true,0x00000000,0x00000000,false),
86 HOWTO(R_386_32, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_32", true,0xffffffff,0xffffffff,false),
87 HOWTO(R_386_PC32, 0,2,32,true, 0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PC32", true,0xffffffff,0xffffffff,true),
88 HOWTO(R_386_GOT32, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOT32", true,0xffffffff,0xffffffff,false),
eb4267a3 89 HOWTO(R_386_PLT32, 0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PLT32", true,0xffffffff,0xffffffff,true),
68241b2b
ILT
90 HOWTO(R_386_COPY, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_COPY", true,0xffffffff,0xffffffff,false),
91 HOWTO(R_386_GLOB_DAT, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GLOB_DAT", true,0xffffffff,0xffffffff,false),
92 HOWTO(R_386_JUMP_SLOT, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_JUMP_SLOT",true,0xffffffff,0xffffffff,false),
93 HOWTO(R_386_RELATIVE, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_RELATIVE", true,0xffffffff,0xffffffff,false),
94 HOWTO(R_386_GOTOFF, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTOFF", true,0xffffffff,0xffffffff,false),
eb4267a3 95 HOWTO(R_386_GOTPC, 0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTPC", true,0xffffffff,0xffffffff,true),
e4b6b3e7
ILT
96};
97
98#ifdef DEBUG_GEN_RELOC
99#define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
100#else
101#define TRACE(str)
102#endif
103
30dc85f1 104static reloc_howto_type *
013dec1a
ILT
105elf_i386_reloc_type_lookup (abfd, code)
106 bfd *abfd;
107 bfd_reloc_code_real_type code;
e4b6b3e7
ILT
108{
109 switch (code)
110 {
111 case BFD_RELOC_NONE:
112 TRACE ("BFD_RELOC_NONE");
113 return &elf_howto_table[ (int)R_386_NONE ];
114
115 case BFD_RELOC_32:
116 TRACE ("BFD_RELOC_32");
117 return &elf_howto_table[ (int)R_386_32 ];
118
119 case BFD_RELOC_32_PCREL:
120 TRACE ("BFD_RELOC_PC32");
121 return &elf_howto_table[ (int)R_386_PC32 ];
122
68241b2b
ILT
123 case BFD_RELOC_386_GOT32:
124 TRACE ("BFD_RELOC_386_GOT32");
125 return &elf_howto_table[ (int)R_386_GOT32 ];
126
127 case BFD_RELOC_386_PLT32:
128 TRACE ("BFD_RELOC_386_PLT32");
129 return &elf_howto_table[ (int)R_386_PLT32 ];
130
131 case BFD_RELOC_386_COPY:
132 TRACE ("BFD_RELOC_386_COPY");
133 return &elf_howto_table[ (int)R_386_COPY ];
134
135 case BFD_RELOC_386_GLOB_DAT:
136 TRACE ("BFD_RELOC_386_GLOB_DAT");
137 return &elf_howto_table[ (int)R_386_GLOB_DAT ];
138
139 case BFD_RELOC_386_JUMP_SLOT:
140 TRACE ("BFD_RELOC_386_JUMP_SLOT");
141 return &elf_howto_table[ (int)R_386_JUMP_SLOT ];
142
143 case BFD_RELOC_386_RELATIVE:
144 TRACE ("BFD_RELOC_386_RELATIVE");
145 return &elf_howto_table[ (int)R_386_RELATIVE ];
146
147 case BFD_RELOC_386_GOTOFF:
148 TRACE ("BFD_RELOC_386_GOTOFF");
149 return &elf_howto_table[ (int)R_386_GOTOFF ];
150
151 case BFD_RELOC_386_GOTPC:
152 TRACE ("BFD_RELOC_386_GOTPC");
153 return &elf_howto_table[ (int)R_386_GOTPC ];
154
e4b6b3e7 155 default:
68241b2b 156 break;
e4b6b3e7
ILT
157 }
158
159 TRACE ("Unknown");
160 return 0;
161}
162
163static void
013dec1a
ILT
164elf_i386_info_to_howto (abfd, cache_ptr, dst)
165 bfd *abfd;
166 arelent *cache_ptr;
167 Elf32_Internal_Rela *dst;
e4b6b3e7 168{
68241b2b 169 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
e4b6b3e7
ILT
170
171 cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
172}
173
174static void
013dec1a
ILT
175elf_i386_info_to_howto_rel (abfd, cache_ptr, dst)
176 bfd *abfd;
177 arelent *cache_ptr;
178 Elf32_Internal_Rel *dst;
e4b6b3e7 179{
68241b2b 180 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
e4b6b3e7
ILT
181
182 cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
183}
013dec1a
ILT
184\f
185/* Functions for the i386 ELF linker. */
186
187/* The name of the dynamic interpreter. This is put in the .interp
188 section. */
189
190#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
191
192/* The size in bytes of an entry in the procedure linkage table. */
193
194#define PLT_ENTRY_SIZE 16
195
196/* The first entry in an absolute procedure linkage table looks like
197 this. See the SVR4 ABI i386 supplement to see how this works. */
198
89f7a04c 199static const bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
013dec1a
ILT
200{
201 0xff, 0x35, /* pushl contents of address */
202 0, 0, 0, 0, /* replaced with address of .got + 4. */
203 0xff, 0x25, /* jmp indirect */
204 0, 0, 0, 0, /* replaced with address of .got + 8. */
205 0, 0, 0, 0 /* pad out to 16 bytes. */
206};
207
208/* Subsequent entries in an absolute procedure linkage table look like
209 this. */
210
89f7a04c 211static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
013dec1a
ILT
212{
213 0xff, 0x25, /* jmp indirect */
214 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
215 0x68, /* pushl immediate */
216 0, 0, 0, 0, /* replaced with offset into relocation table. */
217 0xe9, /* jmp relative */
218 0, 0, 0, 0 /* replaced with offset to start of .plt. */
219};
220
eb4267a3
ILT
221/* The first entry in a PIC procedure linkage table look like this. */
222
89f7a04c 223static const bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] =
eb4267a3
ILT
224{
225 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
226 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
227 0, 0, 0, 0 /* pad out to 16 bytes. */
228};
229
230/* Subsequent entries in a PIC procedure linkage table look like this. */
231
89f7a04c 232static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
eb4267a3
ILT
233{
234 0xff, 0xa3, /* jmp *offset(%ebx) */
235 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
236 0x68, /* pushl immediate */
237 0, 0, 0, 0, /* replaced with offset into relocation table. */
238 0xe9, /* jmp relative */
239 0, 0, 0, 0 /* replaced with offset to start of .plt. */
240};
241
eb4267a3
ILT
242/* Look through the relocs for a section during the first phase, and
243 allocate space in the global offset table or procedure linkage
244 table. */
245
246static boolean
247elf_i386_check_relocs (abfd, info, sec, relocs)
248 bfd *abfd;
249 struct bfd_link_info *info;
250 asection *sec;
251 const Elf_Internal_Rela *relocs;
252{
253 bfd *dynobj;
254 Elf_Internal_Shdr *symtab_hdr;
255 struct elf_link_hash_entry **sym_hashes;
256 bfd_vma *local_got_offsets;
257 const Elf_Internal_Rela *rel;
258 const Elf_Internal_Rela *rel_end;
259 asection *sgot;
260 asection *srelgot;
eb4267a3
ILT
261 asection *sreloc;
262
263 if (info->relocateable)
264 return true;
265
266 dynobj = elf_hash_table (info)->dynobj;
267 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
268 sym_hashes = elf_sym_hashes (abfd);
269 local_got_offsets = elf_local_got_offsets (abfd);
270
271 sgot = NULL;
272 srelgot = NULL;
eb4267a3
ILT
273 sreloc = NULL;
274
275 rel_end = relocs + sec->reloc_count;
276 for (rel = relocs; rel < rel_end; rel++)
277 {
278 long r_symndx;
279 struct elf_link_hash_entry *h;
280
281 r_symndx = ELF32_R_SYM (rel->r_info);
282
283 if (r_symndx < symtab_hdr->sh_info)
284 h = NULL;
285 else
286 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
287
12662be4 288 /* Some relocs require a global offset table. */
eb4267a3
ILT
289 if (dynobj == NULL)
290 {
291 switch (ELF32_R_TYPE (rel->r_info))
292 {
293 case R_386_GOT32:
eb4267a3
ILT
294 case R_386_GOTOFF:
295 case R_386_GOTPC:
296 elf_hash_table (info)->dynobj = dynobj = abfd;
ede4eed4 297 if (! _bfd_elf_create_got_section (dynobj, info))
eb4267a3
ILT
298 return false;
299 break;
300
301 default:
302 break;
303 }
304 }
305
306 switch (ELF32_R_TYPE (rel->r_info))
307 {
308 case R_386_GOT32:
309 /* This symbol requires a global offset table entry. */
310
311 if (sgot == NULL)
312 {
313 sgot = bfd_get_section_by_name (dynobj, ".got");
12662be4
ILT
314 BFD_ASSERT (sgot != NULL);
315 }
316
317 if (srelgot == NULL
318 && (h != NULL || info->shared))
319 {
eb4267a3
ILT
320 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
321 if (srelgot == NULL)
322 {
323 srelgot = bfd_make_section (dynobj, ".rel.got");
324 if (srelgot == NULL
325 || ! bfd_set_section_flags (dynobj, srelgot,
326 (SEC_ALLOC
327 | SEC_LOAD
328 | SEC_HAS_CONTENTS
329 | SEC_IN_MEMORY
330 | SEC_READONLY))
331 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
332 return false;
333 }
eb4267a3
ILT
334 }
335
336 if (h != NULL)
337 {
338 if (h->got_offset != (bfd_vma) -1)
339 {
340 /* We have already allocated space in the .got. */
341 break;
342 }
343 h->got_offset = sgot->_raw_size;
344
345 /* Make sure this symbol is output as a dynamic symbol. */
346 if (h->dynindx == -1)
347 {
348 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
349 return false;
350 }
12662be4
ILT
351
352 srelgot->_raw_size += sizeof (Elf32_External_Rel);
eb4267a3
ILT
353 }
354 else
355 {
356 /* This is a global offset table entry for a local
357 symbol. */
358 if (local_got_offsets == NULL)
359 {
360 size_t size;
361 register int i;
362
363 size = symtab_hdr->sh_info * sizeof (bfd_vma);
364 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
365 if (local_got_offsets == NULL)
366 {
367 bfd_set_error (bfd_error_no_memory);
368 return false;
369 }
370 elf_local_got_offsets (abfd) = local_got_offsets;
371 for (i = 0; i < symtab_hdr->sh_info; i++)
372 local_got_offsets[i] = (bfd_vma) -1;
373 }
374 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
375 {
376 /* We have already allocated space in the .got. */
377 break;
378 }
379 local_got_offsets[r_symndx] = sgot->_raw_size;
12662be4
ILT
380
381 if (info->shared)
382 {
383 /* If we are generating a shared object, we need to
384 output a R_386_RELATIVE reloc so that the dynamic
385 linker can adjust this GOT entry. */
386 srelgot->_raw_size += sizeof (Elf32_External_Rel);
387 }
eb4267a3
ILT
388 }
389
390 sgot->_raw_size += 4;
eb4267a3
ILT
391
392 break;
393
394 case R_386_PLT32:
12662be4
ILT
395 /* This symbol requires a procedure linkage table entry. We
396 actually build the entry in adjust_dynamic_symbol,
397 because this might be a case of linking PIC code without
398 linking in any dynamic objects, in which case we don't
399 need to generate a procedure linkage table after all. */
eb4267a3
ILT
400
401 /* If this is a local symbol, we resolve it directly without
402 creating a procedure linkage table entry. */
403 if (h == NULL)
404 continue;
405
eb4267a3
ILT
406 /* Make sure this symbol is output as a dynamic symbol. */
407 if (h->dynindx == -1)
408 {
409 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
410 return false;
411 }
412
12662be4 413 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
eb4267a3
ILT
414
415 break;
416
417 case R_386_32:
418 case R_386_PC32:
419 if (info->shared
452a5efb
ILT
420 && (sec->flags & SEC_ALLOC) != 0
421 && (r_type != R_386_PC32 || h != NULL))
eb4267a3 422 {
14cac507
ILT
423 /* When creating a shared object, we must copy these
424 reloc types into the output file. We create a reloc
425 section in dynobj and make room for this reloc. */
eb4267a3
ILT
426 if (sreloc == NULL)
427 {
428 const char *name;
429
ede4eed4 430 name = (bfd_elf_string_from_elf_section
eb4267a3
ILT
431 (abfd,
432 elf_elfheader (abfd)->e_shstrndx,
433 elf_section_data (sec)->rel_hdr.sh_name));
434 if (name == NULL)
435 return false;
436
437 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
438 && strcmp (bfd_get_section_name (abfd, sec),
439 name + 4) == 0);
440
441 sreloc = bfd_get_section_by_name (dynobj, name);
442 if (sreloc == NULL)
443 {
444 sreloc = bfd_make_section (dynobj, name);
445 if (sreloc == NULL
446 || ! bfd_set_section_flags (dynobj, sreloc,
447 (SEC_ALLOC
448 | SEC_LOAD
449 | SEC_HAS_CONTENTS
450 | SEC_IN_MEMORY
451 | SEC_READONLY))
452 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
453 return false;
454 }
455 }
456
457 sreloc->_raw_size += sizeof (Elf32_External_Rel);
458 }
459
460 break;
461
462 default:
463 break;
464 }
465 }
013dec1a
ILT
466
467 return true;
468}
469
470/* Adjust a symbol defined by a dynamic object and referenced by a
471 regular object. The current definition is in some section of the
472 dynamic object, but we're not including those sections. We have to
473 change the definition to something the rest of the link can
474 understand. */
475
476static boolean
477elf_i386_adjust_dynamic_symbol (info, h)
478 struct bfd_link_info *info;
479 struct elf_link_hash_entry *h;
480{
481 bfd *dynobj;
482 asection *s;
483 unsigned int power_of_two;
013dec1a
ILT
484
485 dynobj = elf_hash_table (info)->dynobj;
486
487 /* Make sure we know what is going on here. */
3004a68c
ILT
488 BFD_ASSERT (dynobj != NULL
489 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
452a5efb 490 || h->weakdef != NULL
3004a68c
ILT
491 || ((h->elf_link_hash_flags
492 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
493 && (h->elf_link_hash_flags
494 & ELF_LINK_HASH_REF_REGULAR) != 0
495 && (h->elf_link_hash_flags
496 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
013dec1a
ILT
497
498 /* If this is a function, put it in the procedure linkage table. We
499 will fill in the contents of the procedure linkage table later,
500 when we know the address of the .got section. */
12662be4
ILT
501 if (h->type == STT_FUNC
502 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
013dec1a 503 {
12662be4
ILT
504 if (! elf_hash_table (info)->dynamic_sections_created)
505 {
506 /* This case can occur if we saw a PLT32 reloc in an input
507 file, but none of the input files were dynamic objects.
508 In such a case, we don't actually need to build a
509 procedure linkage table, and we can just do a PC32 reloc
510 instead. */
511 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
512 return true;
513 }
514
013dec1a
ILT
515 s = bfd_get_section_by_name (dynobj, ".plt");
516 BFD_ASSERT (s != NULL);
517
12662be4
ILT
518 /* If this is the first .plt entry, make room for the special
519 first entry. */
520 if (s->_raw_size == 0)
521 s->_raw_size += PLT_ENTRY_SIZE;
013dec1a 522
9b09a015
ILT
523 /* If this symbol is not defined in a regular file, and we are
524 not generating a shared library, then set the symbol to this
525 location in the .plt. This is required to make function
526 pointers compare as equal between the normal executable and
527 the shared library. */
528 if (! info->shared
529 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
14cac507
ILT
530 {
531 h->root.u.def.section = s;
532 h->root.u.def.value = s->_raw_size;
533 }
013dec1a 534
12662be4 535 h->plt_offset = s->_raw_size;
013dec1a 536
12662be4
ILT
537 /* Make room for this entry. */
538 s->_raw_size += PLT_ENTRY_SIZE;
013dec1a 539
12662be4
ILT
540 /* We also need to make an entry in the .got.plt section, which
541 will be placed in the .got section by the linker script. */
013dec1a 542
12662be4
ILT
543 s = bfd_get_section_by_name (dynobj, ".got.plt");
544 BFD_ASSERT (s != NULL);
545 s->_raw_size += 4;
013dec1a 546
12662be4 547 /* We also need to make an entry in the .rel.plt section. */
eb4267a3 548
12662be4
ILT
549 s = bfd_get_section_by_name (dynobj, ".rel.plt");
550 BFD_ASSERT (s != NULL);
551 s->_raw_size += sizeof (Elf32_External_Rel);
013dec1a
ILT
552
553 return true;
554 }
555
556 /* If this is a weak symbol, and there is a real definition, the
557 processor independent code will have arranged for us to see the
558 real definition first, and we can just use the same value. */
559 if (h->weakdef != NULL)
560 {
30dc85f1
ILT
561 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
562 || h->weakdef->root.type == bfd_link_hash_defweak);
013dec1a
ILT
563 h->root.u.def.section = h->weakdef->root.u.def.section;
564 h->root.u.def.value = h->weakdef->root.u.def.value;
013dec1a
ILT
565 return true;
566 }
567
568 /* This is a reference to a symbol defined by a dynamic object which
eb4267a3
ILT
569 is not a function. */
570
571 /* If we are creating a shared library, we must presume that the
572 only references to the symbol are via the global offset table.
573 For such cases we need not do anything here; the relocations will
574 be handled correctly by relocate_section. */
575 if (info->shared)
576 return true;
577
578 /* We must allocate the symbol in our .dynbss section, which will
579 become part of the .bss section of the executable. There will be
580 an entry for this symbol in the .dynsym section. The dynamic
581 object will contain position independent code, so all references
582 from the dynamic object to this symbol will go through the global
583 offset table. The dynamic linker will use the .dynsym entry to
584 determine the address it must put in the global offset table, so
585 both the dynamic object and the regular object will refer to the
586 same memory location for the variable. */
013dec1a
ILT
587
588 s = bfd_get_section_by_name (dynobj, ".dynbss");
589 BFD_ASSERT (s != NULL);
590
591 /* If the symbol is currently defined in the .bss section of the
592 dynamic object, then it is OK to simply initialize it to zero.
593 If the symbol is in some other section, we must generate a
594 R_386_COPY reloc to tell the dynamic linker to copy the initial
595 value out of the dynamic object and into the runtime process
596 image. We need to remember the offset into the .rel.bss section
7c6da9ca 597 we are going to use. */
eb4267a3 598 if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
013dec1a
ILT
599 {
600 asection *srel;
601
602 srel = bfd_get_section_by_name (dynobj, ".rel.bss");
603 BFD_ASSERT (srel != NULL);
013dec1a 604 srel->_raw_size += sizeof (Elf32_External_Rel);
eb4267a3 605 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
013dec1a
ILT
606 }
607
608 /* We need to figure out the alignment required for this symbol. I
609 have no idea how ELF linkers handle this. */
7c6da9ca
ILT
610 power_of_two = bfd_log2 (h->size);
611 if (power_of_two > 3)
612 power_of_two = 3;
013dec1a
ILT
613
614 /* Apply the required alignment. */
7c6da9ca
ILT
615 s->_raw_size = BFD_ALIGN (s->_raw_size,
616 (bfd_size_type) (1 << power_of_two));
013dec1a
ILT
617 if (power_of_two > bfd_get_section_alignment (dynobj, s))
618 {
619 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
620 return false;
621 }
622
623 /* Define the symbol as being at this point in the section. */
624 h->root.u.def.section = s;
625 h->root.u.def.value = s->_raw_size;
626
627 /* Increment the section size to make room for the symbol. */
628 s->_raw_size += h->size;
629
630 return true;
631}
632
013dec1a
ILT
633/* Set the sizes of the dynamic sections. */
634
635static boolean
636elf_i386_size_dynamic_sections (output_bfd, info)
637 bfd *output_bfd;
638 struct bfd_link_info *info;
639{
640 bfd *dynobj;
641 asection *s;
eb4267a3
ILT
642 boolean plt;
643 boolean relocs;
644 boolean reltext;
013dec1a
ILT
645
646 dynobj = elf_hash_table (info)->dynobj;
647 BFD_ASSERT (dynobj != NULL);
648
12662be4 649 if (elf_hash_table (info)->dynamic_sections_created)
8af74670 650 {
12662be4
ILT
651 /* Set the contents of the .interp section to the interpreter. */
652 if (! info->shared)
653 {
654 s = bfd_get_section_by_name (dynobj, ".interp");
655 BFD_ASSERT (s != NULL);
656 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
657 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
658 }
659 }
660 else
661 {
662 /* We may have created entries in the .rel.got section.
663 However, if we are not creating the dynamic sections, we will
664 not actually use these entries. Reset the size of .rel.got,
665 which will cause it to get stripped from the output file
666 below. */
667 s = bfd_get_section_by_name (dynobj, ".rel.got");
668 if (s != NULL)
669 s->_raw_size = 0;
8af74670 670 }
013dec1a 671
eb4267a3
ILT
672 /* The check_relocs and adjust_dynamic_symbol entry points have
673 determined the sizes of the various dynamic sections. Allocate
674 memory for them. */
675 plt = false;
676 relocs = false;
677 reltext = false;
678 for (s = dynobj->sections; s != NULL; s = s->next)
679 {
680 const char *name;
681 boolean strip;
682
683 if ((s->flags & SEC_IN_MEMORY) == 0)
684 continue;
685
686 /* It's OK to base decisions on the section name, because none
687 of the dynobj section names depend upon the input files. */
688 name = bfd_get_section_name (dynobj, s);
689
690 strip = false;
691
692 if (strcmp (name, ".plt") == 0)
693 {
694 if (s->_raw_size == 0)
695 {
696 /* Strip this section if we don't need it; see the
697 comment below. */
698 strip = true;
699 }
700 else
701 {
702 /* Remember whether there is a PLT. */
703 plt = true;
704 }
705 }
706 else if (strncmp (name, ".rel", 4) == 0)
707 {
708 if (s->_raw_size == 0)
709 {
710 /* If we don't need this section, strip it from the
711 output file. This is mostly to handle .rel.bss and
712 .rel.plt. We must create both sections in
713 create_dynamic_sections, because they must be created
714 before the linker maps input sections to output
715 sections. The linker does that before
716 adjust_dynamic_symbol is called, and it is that
717 function which decides whether anything needs to go
718 into these sections. */
719 strip = true;
720 }
721 else
722 {
723 asection *target;
724
725 /* Remember whether there are any reloc sections other
726 than .rel.plt. */
727 if (strcmp (name, ".rel.plt") != 0)
728 relocs = true;
729
730 /* If this relocation section applies to a read only
731 section, then we probably need a DT_TEXTREL entry. */
732 target = bfd_get_section_by_name (output_bfd, name + 4);
733 if (target != NULL
734 && (target->flags & SEC_READONLY) != 0)
735 reltext = true;
736
737 /* We use the reloc_count field as a counter if we need
738 to copy relocs into the output file. */
739 s->reloc_count = 0;
740 }
741 }
742 else if (strncmp (name, ".got", 4) != 0)
743 {
744 /* It's not one of our sections, so don't allocate space. */
745 continue;
746 }
747
748 if (strip)
749 {
750 asection **spp;
751
752 for (spp = &s->output_section->owner->sections;
753 *spp != s->output_section;
754 spp = &(*spp)->next)
755 ;
756 *spp = s->output_section->next;
757 --s->output_section->owner->section_count;
758
759 continue;
760 }
013dec1a 761
eb4267a3
ILT
762 /* Allocate memory for the section contents. */
763 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
764 if (s->contents == NULL && s->_raw_size != 0)
765 {
766 bfd_set_error (bfd_error_no_memory);
767 return false;
768 }
769 }
770
12662be4 771 if (elf_hash_table (info)->dynamic_sections_created)
eb4267a3 772 {
12662be4
ILT
773 /* Add some entries to the .dynamic section. We fill in the
774 values later, in elf_i386_finish_dynamic_sections, but we
775 must add the entries now so that we get the correct size for
776 the .dynamic section. The DT_DEBUG entry is filled in by the
777 dynamic linker and used by the debugger. */
778 if (! info->shared)
779 {
780 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
781 return false;
782 }
013dec1a 783
12662be4
ILT
784 if (plt)
785 {
786 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
787 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
788 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL)
789 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
790 return false;
791 }
013dec1a 792
12662be4
ILT
793 if (relocs)
794 {
795 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0)
796 || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0)
797 || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT,
798 sizeof (Elf32_External_Rel)))
799 return false;
800 }
013dec1a 801
12662be4
ILT
802 if (reltext)
803 {
804 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
805 return false;
806 }
eb4267a3
ILT
807 }
808
013dec1a
ILT
809 return true;
810}
811
812/* Relocate an i386 ELF section. */
813
814static boolean
815elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
eb4267a3 816 contents, relocs, local_syms, local_sections)
013dec1a
ILT
817 bfd *output_bfd;
818 struct bfd_link_info *info;
819 bfd *input_bfd;
820 asection *input_section;
821 bfd_byte *contents;
822 Elf_Internal_Rela *relocs;
823 Elf_Internal_Sym *local_syms;
824 asection **local_sections;
825{
eb4267a3 826 bfd *dynobj;
013dec1a 827 Elf_Internal_Shdr *symtab_hdr;
eb4267a3
ILT
828 struct elf_link_hash_entry **sym_hashes;
829 bfd_vma *local_got_offsets;
830 asection *sgot;
831 asection *splt;
832 asection *sreloc;
013dec1a
ILT
833 Elf_Internal_Rela *rel;
834 Elf_Internal_Rela *relend;
835
eb4267a3 836 dynobj = elf_hash_table (info)->dynobj;
013dec1a 837 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
eb4267a3
ILT
838 sym_hashes = elf_sym_hashes (input_bfd);
839 local_got_offsets = elf_local_got_offsets (input_bfd);
840
841 sgot = NULL;
842 splt = NULL;
843 sreloc = NULL;
013dec1a
ILT
844
845 rel = relocs;
846 relend = relocs + input_section->reloc_count;
847 for (; rel < relend; rel++)
848 {
849 int r_type;
82b1edf7 850 reloc_howto_type *howto;
013dec1a
ILT
851 long r_symndx;
852 struct elf_link_hash_entry *h;
853 Elf_Internal_Sym *sym;
854 asection *sec;
855 bfd_vma relocation;
856 bfd_reloc_status_type r;
857
858 r_type = ELF32_R_TYPE (rel->r_info);
859 if (r_type < 0 || r_type >= (int) R_386_max)
860 {
861 bfd_set_error (bfd_error_bad_value);
862 return false;
863 }
864 howto = elf_howto_table + r_type;
865
866 r_symndx = ELF32_R_SYM (rel->r_info);
867
868 if (info->relocateable)
869 {
870 /* This is a relocateable link. We don't have to change
871 anything, unless the reloc is against a section symbol,
872 in which case we have to adjust according to where the
873 section symbol winds up in the output section. */
874 if (r_symndx < symtab_hdr->sh_info)
875 {
876 sym = local_syms + r_symndx;
877 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
878 {
879 bfd_vma val;
880
881 sec = local_sections[r_symndx];
882 val = bfd_get_32 (input_bfd, contents + rel->r_offset);
883 val += sec->output_offset + sym->st_value;
884 bfd_put_32 (input_bfd, val, contents + rel->r_offset);
885 }
886 }
887
888 continue;
889 }
890
891 /* This is a final link. */
892 h = NULL;
893 sym = NULL;
894 sec = NULL;
895 if (r_symndx < symtab_hdr->sh_info)
896 {
897 sym = local_syms + r_symndx;
898 sec = local_sections[r_symndx];
899 relocation = (sec->output_section->vma
900 + sec->output_offset
901 + sym->st_value);
902 }
903 else
904 {
eb4267a3 905 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
30dc85f1
ILT
906 if (h->root.type == bfd_link_hash_defined
907 || h->root.type == bfd_link_hash_defweak)
013dec1a
ILT
908 {
909 sec = h->root.u.def.section;
9b09a015
ILT
910 if (r_type == R_386_GOTPC
911 || (r_type == R_386_PLT32
912 && h->plt_offset != (bfd_vma) -1)
913 || (r_type == R_386_GOT32
452a5efb
ILT
914 && elf_hash_table (info)->dynamic_sections_created
915 && (! info->shared
916 || ! info->symbolic
917 || (h->elf_link_hash_flags
918 & ELF_LINK_HASH_DEF_REGULAR) == 0))
9b09a015
ILT
919 || (info->shared
920 && (r_type == R_386_32
921 || r_type == R_386_PC32)
922 && (input_section->flags & SEC_ALLOC) != 0))
923 {
924 /* In these cases, we don't need the relocation
925 value. We check specially because in some
926 obscure cases sec->output_section will be NULL. */
927 relocation = 0;
928 }
929 else
930 relocation = (h->root.u.def.value
931 + sec->output_section->vma
932 + sec->output_offset);
013dec1a 933 }
30dc85f1 934 else if (h->root.type == bfd_link_hash_undefweak)
013dec1a 935 relocation = 0;
452a5efb 936 else if (info->shared && !info->symbolic)
eb4267a3 937 relocation = 0;
013dec1a
ILT
938 else
939 {
940 if (! ((*info->callbacks->undefined_symbol)
941 (info, h->root.root.string, input_bfd,
942 input_section, rel->r_offset)))
943 return false;
944 relocation = 0;
945 }
946 }
947
eb4267a3
ILT
948 switch (r_type)
949 {
950 case R_386_GOT32:
951 /* Relocation is to the entry for this symbol in the global
952 offset table. */
953 if (sgot == NULL)
954 {
955 sgot = bfd_get_section_by_name (dynobj, ".got");
956 BFD_ASSERT (sgot != NULL);
957 }
958
959 if (h != NULL)
960 {
12662be4
ILT
961 bfd_vma off;
962
963 off = h->got_offset;
964 BFD_ASSERT (off != (bfd_vma) -1);
965
452a5efb
ILT
966 if (! elf_hash_table (info)->dynamic_sections_created
967 || (info->shared
968 && info->symbolic
969 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
12662be4 970 {
452a5efb
ILT
971 /* This is actually a static link, or it is a
972 -Bsymbolic link and the symbol is defined
973 locally. We must initialize this entry in the
974 global offset table. Since the offset must
975 always be a multiple of 4, we use the least
976 significant bit to record whether we have
977 initialized it already.
12662be4
ILT
978
979 When doing a dynamic link, we create a .rel.got
980 relocation entry to initialize the value. This
981 is done in the finish_dynamic_symbol routine. */
982 if ((off & 1) != 0)
983 off &= ~1;
984 else
985 {
986 bfd_put_32 (output_bfd, relocation,
987 sgot->contents + off);
988 h->got_offset |= 1;
989 }
990 }
991
992 relocation = sgot->output_offset + off;
eb4267a3
ILT
993 }
994 else
995 {
996 bfd_vma off;
997
998 BFD_ASSERT (local_got_offsets != NULL
999 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1000
1001 off = local_got_offsets[r_symndx];
1002
1003 /* The offset must always be a multiple of 4. We use
1004 the least significant bit to record whether we have
1005 already generated the necessary reloc. */
1006 if ((off & 1) != 0)
1007 off &= ~1;
1008 else
1009 {
eb4267a3
ILT
1010 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1011
12662be4
ILT
1012 if (info->shared)
1013 {
1014 asection *srelgot;
1015 Elf_Internal_Rel outrel;
1016
1017 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
1018 BFD_ASSERT (srelgot != NULL);
1019
1020 outrel.r_offset = (sgot->output_section->vma
1021 + sgot->output_offset
1022 + off);
1023 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
1024 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1025 (((Elf32_External_Rel *)
1026 srelgot->contents)
1027 + srelgot->reloc_count));
1028 ++srelgot->reloc_count;
1029 }
eb4267a3
ILT
1030
1031 local_got_offsets[r_symndx] |= 1;
1032 }
1033
1034 relocation = sgot->output_offset + off;
1035 }
1036
1037 break;
1038
1039 case R_386_GOTOFF:
1040 /* Relocation is relative to the start of the global offset
1041 table. */
1042
1043 if (sgot == NULL)
1044 {
1045 sgot = bfd_get_section_by_name (dynobj, ".got");
1046 BFD_ASSERT (sgot != NULL);
1047 }
1048
1049 /* Note that sgot->output_offset is not involved in this
1050 calculation. We always want the start of .got. If we
1051 defined _GLOBAL_OFFSET_TABLE in a different way, as is
1052 permitted by the ABI, we might have to change this
1053 calculation. */
1054 relocation -= sgot->output_section->vma;
1055
1056 break;
1057
1058 case R_386_GOTPC:
1059 /* Use global offset table as symbol value. */
1060
1061 if (sgot == NULL)
1062 {
1063 sgot = bfd_get_section_by_name (dynobj, ".got");
1064 BFD_ASSERT (sgot != NULL);
1065 }
1066
1067 relocation = sgot->output_section->vma;
1068
1069 break;
1070
1071 case R_386_PLT32:
1072 /* Relocation is to the entry for this symbol in the
1073 procedure linkage table. */
1074
1075 /* Resolve a PLT32 reloc again a local symbol directly,
1076 without using the procedure linkage table. */
1077 if (h == NULL)
1078 break;
1079
12662be4
ILT
1080 if (h->plt_offset == (bfd_vma) -1)
1081 {
1082 /* We didn't make a PLT entry for this symbol. This
452a5efb
ILT
1083 happens when statically linking PIC code, or when
1084 using -Bsymbolic. */
12662be4
ILT
1085 break;
1086 }
1087
eb4267a3
ILT
1088 if (splt == NULL)
1089 {
1090 splt = bfd_get_section_by_name (dynobj, ".plt");
1091 BFD_ASSERT (splt != NULL);
1092 }
1093
eb4267a3
ILT
1094 relocation = (splt->output_section->vma
1095 + splt->output_offset
1096 + h->plt_offset);
1097
1098 break;
1099
1100 case R_386_32:
1101 case R_386_PC32:
1102 if (info->shared
1d5d75e9
ILT
1103 && (input_section->flags & SEC_ALLOC) != 0
1104 && (r_type != R_386_PC32 || h != NULL))
eb4267a3
ILT
1105 {
1106 Elf_Internal_Rel outrel;
1107
1108 /* When generating a shared object, these relocations
1109 are copied into the output file to be resolved at run
1110 time. */
1111
1112 if (sreloc == NULL)
1113 {
1d5d75e9
ILT
1114 const char *name;
1115
1116 name = (bfd_elf_string_from_elf_section
eb4267a3
ILT
1117 (input_bfd,
1118 elf_elfheader (input_bfd)->e_shstrndx,
1119 elf_section_data (input_section)->rel_hdr.sh_name));
1d5d75e9 1120 if (name == NULL)
eb4267a3
ILT
1121 return false;
1122
1d5d75e9 1123 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
eb4267a3
ILT
1124 && strcmp (bfd_get_section_name (input_bfd,
1125 input_section),
1d5d75e9 1126 name + 4) == 0);
eb4267a3 1127
1d5d75e9 1128 sreloc = bfd_get_section_by_name (dynobj, name);
eb4267a3
ILT
1129 BFD_ASSERT (sreloc != NULL);
1130 }
1131
1132 outrel.r_offset = (rel->r_offset
1133 + input_section->output_section->vma
1134 + input_section->output_offset);
1135 if (r_type == R_386_PC32)
1136 {
1d5d75e9
ILT
1137 BFD_ASSERT (h != NULL && h->dynindx != -1);
1138 outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_PC32);
eb4267a3
ILT
1139 }
1140 else
1141 {
1142 if (h == NULL)
1143 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
1144 else
1145 {
1146 BFD_ASSERT (h->dynindx != (bfd_vma) -1);
1147 outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_32);
1148 }
1149 }
1150
1151 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1152 (((Elf32_External_Rel *)
1153 sreloc->contents)
1154 + sreloc->reloc_count));
1155 ++sreloc->reloc_count;
1156
1157 /* If this reloc is against an external symbol, we do
1158 not want to fiddle with the addend. Otherwise, we
1159 need to include the symbol value so that it becomes
1160 an addend for the dynamic reloc. */
1161 if (h != NULL)
1162 continue;
1163 }
1164
1165 break;
1166
1167 default:
1168 break;
1169 }
1170
013dec1a
ILT
1171 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1172 contents, rel->r_offset,
1173 relocation, (bfd_vma) 0);
1174
1175 if (r != bfd_reloc_ok)
1176 {
1177 switch (r)
1178 {
1179 default:
1180 case bfd_reloc_outofrange:
1181 abort ();
1182 case bfd_reloc_overflow:
1183 {
1184 const char *name;
1185
1186 if (h != NULL)
1187 name = h->root.root.string;
1188 else
1189 {
ede4eed4
KR
1190 name = bfd_elf_string_from_elf_section (input_bfd,
1191 symtab_hdr->sh_link,
1192 sym->st_name);
013dec1a
ILT
1193 if (name == NULL)
1194 return false;
1195 if (*name == '\0')
1196 name = bfd_section_name (input_bfd, sec);
1197 }
1198 if (! ((*info->callbacks->reloc_overflow)
1199 (info, name, howto->name, (bfd_vma) 0,
1200 input_bfd, input_section, rel->r_offset)))
1201 return false;
1202 }
1203 break;
1204 }
1205 }
1206 }
1207
1208 return true;
1209}
1210
1211/* Finish up dynamic symbol handling. We set the contents of various
1212 dynamic sections here. */
1213
1214static boolean
1215elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
1216 bfd *output_bfd;
1217 struct bfd_link_info *info;
1218 struct elf_link_hash_entry *h;
1219 Elf_Internal_Sym *sym;
1220{
eb4267a3 1221 bfd *dynobj;
013dec1a 1222
eb4267a3 1223 dynobj = elf_hash_table (info)->dynobj;
013dec1a 1224
eb4267a3 1225 if (h->plt_offset != (bfd_vma) -1)
013dec1a
ILT
1226 {
1227 asection *splt;
1228 asection *sgot;
1229 asection *srel;
1230 bfd_vma plt_index;
1231 bfd_vma got_offset;
1232 Elf_Internal_Rel rel;
1233
eb4267a3
ILT
1234 /* This symbol has an entry in the procedure linkage table. Set
1235 it up. */
013dec1a 1236
eb4267a3
ILT
1237 BFD_ASSERT (h->dynindx != -1);
1238
1239 splt = bfd_get_section_by_name (dynobj, ".plt");
1240 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1241 srel = bfd_get_section_by_name (dynobj, ".rel.plt");
1242 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
013dec1a
ILT
1243
1244 /* Get the index in the procedure linkage table which
1245 corresponds to this symbol. This is the index of this symbol
1246 in all the symbols for which we are making plt entries. The
1247 first entry in the procedure linkage table is reserved. */
eb4267a3 1248 plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1;
013dec1a
ILT
1249
1250 /* Get the offset into the .got table of the entry that
1251 corresponds to this function. Each .got entry is 4 bytes.
1252 The first three are reserved. */
1253 got_offset = (plt_index + 3) * 4;
1254
1255 /* Fill in the entry in the procedure linkage table. */
eb4267a3
ILT
1256 if (! info->shared)
1257 {
1258 memcpy (splt->contents + h->plt_offset, elf_i386_plt_entry,
1259 PLT_ENTRY_SIZE);
1260 bfd_put_32 (output_bfd,
1261 (sgot->output_section->vma
1262 + sgot->output_offset
1263 + got_offset),
1264 splt->contents + h->plt_offset + 2);
1265 }
1266 else
1267 {
1268 memcpy (splt->contents + h->plt_offset, elf_i386_pic_plt_entry,
1269 PLT_ENTRY_SIZE);
1270 bfd_put_32 (output_bfd, got_offset,
1271 splt->contents + h->plt_offset + 2);
1272 }
1273
013dec1a 1274 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
eb4267a3
ILT
1275 splt->contents + h->plt_offset + 7);
1276 bfd_put_32 (output_bfd, - (h->plt_offset + PLT_ENTRY_SIZE),
1277 splt->contents + h->plt_offset + 12);
013dec1a
ILT
1278
1279 /* Fill in the entry in the global offset table. */
1280 bfd_put_32 (output_bfd,
1281 (splt->output_section->vma
1282 + splt->output_offset
eb4267a3 1283 + h->plt_offset
013dec1a
ILT
1284 + 6),
1285 sgot->contents + got_offset);
1286
1287 /* Fill in the entry in the .rel.plt section. */
1288 rel.r_offset = (sgot->output_section->vma
1289 + sgot->output_offset
1290 + got_offset);
1291 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
1292 bfd_elf32_swap_reloc_out (output_bfd, &rel,
1293 ((Elf32_External_Rel *) srel->contents
1294 + plt_index));
1295
eb4267a3
ILT
1296 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1297 {
1298 /* Mark the symbol as undefined, rather than as defined in
1299 the .plt section. Leave the value alone. */
1300 sym->st_shndx = SHN_UNDEF;
1301 }
013dec1a 1302 }
eb4267a3
ILT
1303
1304 if (h->got_offset != (bfd_vma) -1)
013dec1a 1305 {
eb4267a3
ILT
1306 asection *sgot;
1307 asection *srel;
1308 Elf_Internal_Rel rel;
013dec1a 1309
eb4267a3
ILT
1310 /* This symbol has an entry in the global offset table. Set it
1311 up. */
1312
1313 BFD_ASSERT (h->dynindx != -1);
013dec1a 1314
eb4267a3
ILT
1315 sgot = bfd_get_section_by_name (dynobj, ".got");
1316 srel = bfd_get_section_by_name (dynobj, ".rel.got");
1317 BFD_ASSERT (sgot != NULL && srel != NULL);
1318
eb4267a3
ILT
1319 rel.r_offset = (sgot->output_section->vma
1320 + sgot->output_offset
452a5efb
ILT
1321 + (h->got_offset &~ 1));
1322
1323 /* If this is a -Bsymbolic link, and the symbol is defined
1324 locally, we just want to emit a RELATIVE reloc. The entry in
1325 the global offset table will already have been initialized in
1326 the relocate_section function. */
1327 if (info->shared
1328 && info->symbolic
1329 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1330 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
1331 else
1332 {
1333 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
1334 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
1335 }
1336
eb4267a3
ILT
1337 bfd_elf32_swap_reloc_out (output_bfd, &rel,
1338 ((Elf32_External_Rel *) srel->contents
1339 + srel->reloc_count));
1340 ++srel->reloc_count;
1341 }
1342
1343 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1344 {
1345 asection *s;
1346 Elf_Internal_Rel rel;
1347
1348 /* This symbol needs a copy reloc. Set it up. */
1349
1350 BFD_ASSERT (h->dynindx != -1
30dc85f1
ILT
1351 && (h->root.type == bfd_link_hash_defined
1352 || h->root.type == bfd_link_hash_defweak));
eb4267a3
ILT
1353
1354 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1355 ".rel.bss");
1356 BFD_ASSERT (s != NULL);
1357
1358 rel.r_offset = (h->root.u.def.value
1359 + h->root.u.def.section->output_section->vma
1360 + h->root.u.def.section->output_offset);
1361 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
1362 bfd_elf32_swap_reloc_out (output_bfd, &rel,
1363 ((Elf32_External_Rel *) s->contents
1364 + s->reloc_count));
1365 ++s->reloc_count;
013dec1a
ILT
1366 }
1367
eb4267a3
ILT
1368 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1369 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1370 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1371 sym->st_shndx = SHN_ABS;
1372
013dec1a
ILT
1373 return true;
1374}
1375
1376/* Finish up the dynamic sections. */
1377
1378static boolean
1379elf_i386_finish_dynamic_sections (output_bfd, info)
1380 bfd *output_bfd;
1381 struct bfd_link_info *info;
1382{
eb4267a3 1383 bfd *dynobj;
013dec1a
ILT
1384 asection *sgot;
1385 asection *sdyn;
013dec1a 1386
eb4267a3
ILT
1387 dynobj = elf_hash_table (info)->dynobj;
1388
eb4267a3 1389 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
12662be4 1390 BFD_ASSERT (sgot != NULL);
eb4267a3 1391 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
013dec1a 1392
12662be4 1393 if (elf_hash_table (info)->dynamic_sections_created)
013dec1a 1394 {
12662be4
ILT
1395 asection *splt;
1396 Elf32_External_Dyn *dyncon, *dynconend;
013dec1a 1397
12662be4
ILT
1398 splt = bfd_get_section_by_name (dynobj, ".plt");
1399 BFD_ASSERT (splt != NULL && sdyn != NULL);
013dec1a 1400
12662be4
ILT
1401 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1402 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1403 for (; dyncon < dynconend; dyncon++)
013dec1a 1404 {
12662be4
ILT
1405 Elf_Internal_Dyn dyn;
1406 const char *name;
1407 asection *s;
eb4267a3 1408
12662be4 1409 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
eb4267a3 1410
12662be4 1411 switch (dyn.d_tag)
013dec1a 1412 {
12662be4
ILT
1413 default:
1414 break;
1415
1416 case DT_PLTGOT:
1417 name = ".got";
1418 goto get_vma;
1419 case DT_JMPREL:
1420 name = ".rel.plt";
1421 get_vma:
1422 s = bfd_get_section_by_name (output_bfd, name);
1423 BFD_ASSERT (s != NULL);
1424 dyn.d_un.d_ptr = s->vma;
1425 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1426 break;
1427
1428 case DT_PLTRELSZ:
1429 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
1430 BFD_ASSERT (s != NULL);
013dec1a 1431 if (s->_cooked_size != 0)
12662be4 1432 dyn.d_un.d_val = s->_cooked_size;
013dec1a 1433 else
12662be4
ILT
1434 dyn.d_un.d_val = s->_raw_size;
1435 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1436 break;
1437
1438 case DT_RELSZ:
1439 /* My reading of the SVR4 ABI indicates that the
1440 procedure linkage table relocs (DT_JMPREL) should be
1441 included in the overall relocs (DT_REL). This is
1442 what Solaris does. However, UnixWare can not handle
1443 that case. Therefore, we override the DT_RELSZ entry
1444 here to make it not include the JMPREL relocs. Since
1445 the linker script arranges for .rel.plt to follow all
1446 other relocation sections, we don't have to worry
1447 about changing the DT_REL entry. */
1448 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
1449 if (s != NULL)
1450 {
1451 if (s->_cooked_size != 0)
1452 dyn.d_un.d_val -= s->_cooked_size;
1453 else
1454 dyn.d_un.d_val -= s->_raw_size;
1455 }
1456 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1457 break;
013dec1a 1458 }
013dec1a 1459 }
013dec1a 1460
12662be4
ILT
1461 /* Fill in the first entry in the procedure linkage table. */
1462 if (splt->_raw_size > 0)
eb4267a3 1463 {
12662be4
ILT
1464 if (info->shared)
1465 memcpy (splt->contents, elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE);
1466 else
1467 {
1468 memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE);
1469 bfd_put_32 (output_bfd,
1470 sgot->output_section->vma + sgot->output_offset + 4,
1471 splt->contents + 2);
1472 bfd_put_32 (output_bfd,
1473 sgot->output_section->vma + sgot->output_offset + 8,
1474 splt->contents + 8);
1475 }
eb4267a3 1476 }
12662be4
ILT
1477
1478 /* UnixWare sets the entsize of .plt to 4, although that doesn't
1479 really seem like the right value. */
1480 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
013dec1a
ILT
1481 }
1482
1483 /* Fill in the first three entries in the global offset table. */
1484 if (sgot->_raw_size > 0)
1485 {
12662be4
ILT
1486 if (sdyn == NULL)
1487 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1488 else
1489 bfd_put_32 (output_bfd,
1490 sdyn->output_section->vma + sdyn->output_offset,
1491 sgot->contents);
013dec1a
ILT
1492 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1493 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1494 }
1495
1496 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1497
013dec1a
ILT
1498 return true;
1499}
e4b6b3e7
ILT
1500
1501#define TARGET_LITTLE_SYM bfd_elf32_i386_vec
1502#define TARGET_LITTLE_NAME "elf32-i386"
1503#define ELF_ARCH bfd_arch_i386
68241b2b 1504#define ELF_MACHINE_CODE EM_386
e4b6b3e7
ILT
1505#define elf_info_to_howto elf_i386_info_to_howto
1506#define elf_info_to_howto_rel elf_i386_info_to_howto_rel
1507#define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
68241b2b 1508#define ELF_MAXPAGESIZE 0x1000
013dec1a 1509#define elf_backend_create_dynamic_sections \
ede4eed4 1510 _bfd_elf_create_dynamic_sections
eb4267a3 1511#define elf_backend_check_relocs elf_i386_check_relocs
013dec1a
ILT
1512#define elf_backend_adjust_dynamic_symbol \
1513 elf_i386_adjust_dynamic_symbol
1514#define elf_backend_size_dynamic_sections \
1515 elf_i386_size_dynamic_sections
1516#define elf_backend_relocate_section elf_i386_relocate_section
1517#define elf_backend_finish_dynamic_symbol \
1518 elf_i386_finish_dynamic_symbol
1519#define elf_backend_finish_dynamic_sections \
1520 elf_i386_finish_dynamic_sections
ede4eed4
KR
1521#define elf_backend_want_got_plt 1
1522#define elf_backend_plt_readonly 0
1523#define elf_backend_want_plt_sym 0
e4b6b3e7
ILT
1524
1525#include "elf32-target.h"
This page took 0.158094 seconds and 4 git commands to generate.