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