* elf32-i386.c (elf_i386_adjust_dynamic_symbol): Correct
[external/binutils.git] / bfd / elf32-i386.c
1 /* Intel 80386/80486-specific support for 32-bit ELF
2    Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "bfdlink.h"
23 #include "libbfd.h"
24 #include "libelf.h"
25
26 static reloc_howto_type *elf_i386_reloc_type_lookup
27   PARAMS ((bfd *, bfd_reloc_code_real_type));
28 static void elf_i386_info_to_howto
29   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
30 static void elf_i386_info_to_howto_rel
31   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
32 static boolean elf_i386_create_dynamic_sections
33   PARAMS ((bfd *, struct bfd_link_info *));
34 static boolean elf_i386_create_got_section
35   PARAMS ((bfd *, struct bfd_link_info *));
36 static boolean elf_i386_check_relocs
37   PARAMS ((bfd *, struct bfd_link_info *, asection *,
38            const Elf_Internal_Rela *));
39 static boolean elf_i386_adjust_dynamic_symbol
40   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
41 static boolean elf_i386_size_dynamic_sections
42   PARAMS ((bfd *, struct bfd_link_info *));
43 static boolean elf_i386_relocate_section
44   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
45            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
46 static boolean elf_i386_finish_dynamic_symbol
47   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
48            Elf_Internal_Sym *));
49 static boolean elf_i386_finish_dynamic_sections
50   PARAMS ((bfd *, struct bfd_link_info *));
51
52 #define USE_REL 1               /* 386 uses REL relocations instead of RELA */
53
54 enum reloc_type
55   {
56     R_386_NONE = 0,
57     R_386_32,
58     R_386_PC32,
59     R_386_GOT32,
60     R_386_PLT32,
61     R_386_COPY,
62     R_386_GLOB_DAT,
63     R_386_JUMP_SLOT,
64     R_386_RELATIVE,
65     R_386_GOTOFF,
66     R_386_GOTPC,
67     R_386_max
68   };
69
70 #if 0
71 static CONST char *CONST reloc_type_names[] =
72 {
73   "R_386_NONE",
74   "R_386_32",
75   "R_386_PC32",
76   "R_386_GOT32",
77   "R_386_PLT32",
78   "R_386_COPY",
79   "R_386_GLOB_DAT",
80   "R_386_JUMP_SLOT",
81   "R_386_RELATIVE",
82   "R_386_GOTOFF",
83   "R_386_GOTPC",
84 };
85 #endif
86
87 static reloc_howto_type elf_howto_table[]=
88 {
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),
93   HOWTO(R_386_PLT32,     0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PLT32",    true,0xffffffff,0xffffffff,true),
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),
99   HOWTO(R_386_GOTPC,     0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTPC",    true,0xffffffff,0xffffffff,true),
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
108 static reloc_howto_type *
109 elf_i386_reloc_type_lookup (abfd, code)
110      bfd *abfd;
111      bfd_reloc_code_real_type code;
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
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
159     default:
160       break;
161     }
162
163   TRACE ("Unknown");
164   return 0;
165 }
166
167 static void
168 elf_i386_info_to_howto (abfd, cache_ptr, dst)
169      bfd                *abfd;
170      arelent            *cache_ptr;
171      Elf32_Internal_Rela *dst;
172 {
173   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
174
175   cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
176 }
177
178 static void
179 elf_i386_info_to_howto_rel (abfd, cache_ptr, dst)
180      bfd                *abfd;
181      arelent            *cache_ptr;
182      Elf32_Internal_Rel *dst;
183 {
184   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
185
186   cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
187 }
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
203 static const bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
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
215 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
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
225 /* The first entry in a PIC procedure linkage table look like this.  */
226
227 static const bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] =
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
236 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
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
246 /* Create dynamic sections when linking against a dynamic object.  */
247
248 static boolean
249 elf_i386_create_dynamic_sections (abfd, info)
250      bfd *abfd;
251      struct bfd_link_info *info;
252 {
253   flagword flags;
254   register asection *s;
255
256   /* We need to create .plt, .rel.plt, .got, .got.plt, .dynbss, and
257      .rel.bss sections.  */
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
273   if (! elf_i386_create_got_section (abfd, info))
274     return false;
275
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
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
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
314 static boolean
315 elf_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
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
369 static boolean
370 elf_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;
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;
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
411       /* Some relocs require a global offset table.  */
412       if (dynobj == NULL)
413         {
414           switch (ELF32_R_TYPE (rel->r_info))
415             {
416             case R_386_GOT32:
417             case R_386_GOTOFF:
418             case R_386_GOTPC:
419               elf_hash_table (info)->dynobj = dynobj = abfd;
420               if (! elf_i386_create_got_section (dynobj, info))
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");
437               BFD_ASSERT (sgot != NULL);
438             }
439
440           if (srelgot == NULL
441               && (h != NULL || info->shared))
442             {
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                 }
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                 }
474
475               srelgot->_raw_size += sizeof (Elf32_External_Rel);
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;
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                 }
511             }
512
513           sgot->_raw_size += 4;
514
515           break;
516
517         case R_386_PLT32:
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.  */
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
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
536           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
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             {
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.  */
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     }
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
598 static boolean
599 elf_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;
606
607   dynobj = elf_hash_table (info)->dynobj;
608
609   /* Make sure we know what is going on here.  */
610   BFD_ASSERT (dynobj != NULL);
611   BFD_ASSERT ((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
618                   && (h->root.type == bfd_link_hash_defined
619                       || h->root.type == bfd_link_hash_defweak)
620                   && (h->root.u.def.section->owner == NULL
621                       || ((elf_elfheader (h->root.u.def.section->owner)->e_type
622                            == ET_DYN)
623                           && (bfd_get_flavour (h->root.u.def.section->owner)
624                               == bfd_target_elf_flavour)
625                           && h->root.u.def.section->output_section == NULL))));
626
627   /* If this is a function, put it in the procedure linkage table.  We
628      will fill in the contents of the procedure linkage table later,
629      when we know the address of the .got section.  */
630   if (h->type == STT_FUNC
631       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
632     {
633       if (! elf_hash_table (info)->dynamic_sections_created)
634         {
635           /* This case can occur if we saw a PLT32 reloc in an input
636              file, but none of the input files were dynamic objects.
637              In such a case, we don't actually need to build a
638              procedure linkage table, and we can just do a PC32 reloc
639              instead.  */
640           BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
641           return true;
642         }
643
644       s = bfd_get_section_by_name (dynobj, ".plt");
645       BFD_ASSERT (s != NULL);
646
647       /* If this is the first .plt entry, make room for the special
648          first entry.  */
649       if (s->_raw_size == 0)
650         s->_raw_size += PLT_ENTRY_SIZE;
651
652       /* If this symbol is not defined in a regular file, and we are
653          not generating a shared library, then set the symbol to this
654          location in the .plt.  This is required to make function
655          pointers compare as equal between the normal executable and
656          the shared library.  */
657       if (! info->shared
658           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
659         {
660           h->root.u.def.section = s;
661           h->root.u.def.value = s->_raw_size;
662         }
663
664       h->plt_offset = s->_raw_size;
665
666       /* Make room for this entry.  */
667       s->_raw_size += PLT_ENTRY_SIZE;
668
669       /* We also need to make an entry in the .got.plt section, which
670          will be placed in the .got section by the linker script.  */
671
672       s = bfd_get_section_by_name (dynobj, ".got.plt");
673       BFD_ASSERT (s != NULL);
674       s->_raw_size += 4;
675
676       /* We also need to make an entry in the .rel.plt section.  */
677
678       s = bfd_get_section_by_name (dynobj, ".rel.plt");
679       BFD_ASSERT (s != NULL);
680       s->_raw_size += sizeof (Elf32_External_Rel);
681
682       return true;
683     }
684
685   /* If this is a weak symbol, and there is a real definition, the
686      processor independent code will have arranged for us to see the
687      real definition first, and we can just use the same value.  */
688   if (h->weakdef != NULL)
689     {
690       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
691                   || h->weakdef->root.type == bfd_link_hash_defweak);
692       h->root.u.def.section = h->weakdef->root.u.def.section;
693       h->root.u.def.value = h->weakdef->root.u.def.value;
694       return true;
695     }
696
697   /* This is a reference to a symbol defined by a dynamic object which
698      is not a function.  */
699
700   /* If we are creating a shared library, we must presume that the
701      only references to the symbol are via the global offset table.
702      For such cases we need not do anything here; the relocations will
703      be handled correctly by relocate_section.  */
704   if (info->shared)
705     return true;
706
707   /* We must allocate the symbol in our .dynbss section, which will
708      become part of the .bss section of the executable.  There will be
709      an entry for this symbol in the .dynsym section.  The dynamic
710      object will contain position independent code, so all references
711      from the dynamic object to this symbol will go through the global
712      offset table.  The dynamic linker will use the .dynsym entry to
713      determine the address it must put in the global offset table, so
714      both the dynamic object and the regular object will refer to the
715      same memory location for the variable.  */
716
717   s = bfd_get_section_by_name (dynobj, ".dynbss");
718   BFD_ASSERT (s != NULL);
719
720   /* If the symbol is currently defined in the .bss section of the
721      dynamic object, then it is OK to simply initialize it to zero.
722      If the symbol is in some other section, we must generate a
723      R_386_COPY reloc to tell the dynamic linker to copy the initial
724      value out of the dynamic object and into the runtime process
725      image.  We need to remember the offset into the .rel.bss section
726      we are going to use.  */
727   if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
728     {
729       asection *srel;
730
731       srel = bfd_get_section_by_name (dynobj, ".rel.bss");
732       BFD_ASSERT (srel != NULL);
733       srel->_raw_size += sizeof (Elf32_External_Rel);
734       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
735     }
736
737   /* We need to figure out the alignment required for this symbol.  I
738      have no idea how ELF linkers handle this.  */
739   power_of_two = bfd_log2 (h->size);
740   if (power_of_two > 3)
741     power_of_two = 3;
742
743   /* Apply the required alignment.  */
744   s->_raw_size = BFD_ALIGN (s->_raw_size,
745                             (bfd_size_type) (1 << power_of_two));
746   if (power_of_two > bfd_get_section_alignment (dynobj, s))
747     {
748       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
749         return false;
750     }
751
752   /* Define the symbol as being at this point in the section.  */
753   h->root.u.def.section = s;
754   h->root.u.def.value = s->_raw_size;
755
756   /* Increment the section size to make room for the symbol.  */
757   s->_raw_size += h->size;
758
759   return true;
760 }
761
762 /* Set the sizes of the dynamic sections.  */
763
764 static boolean
765 elf_i386_size_dynamic_sections (output_bfd, info)
766      bfd *output_bfd;
767      struct bfd_link_info *info;
768 {
769   bfd *dynobj;
770   asection *s;
771   boolean plt;
772   boolean relocs;
773   boolean reltext;
774
775   dynobj = elf_hash_table (info)->dynobj;
776   BFD_ASSERT (dynobj != NULL);
777
778   if (elf_hash_table (info)->dynamic_sections_created)
779     {
780       /* Set the contents of the .interp section to the interpreter.  */
781       if (! info->shared)
782         {
783           s = bfd_get_section_by_name (dynobj, ".interp");
784           BFD_ASSERT (s != NULL);
785           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
786           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
787         }
788     }
789   else
790     {
791       /* We may have created entries in the .rel.got section.
792          However, if we are not creating the dynamic sections, we will
793          not actually use these entries.  Reset the size of .rel.got,
794          which will cause it to get stripped from the output file
795          below.  */
796       s = bfd_get_section_by_name (dynobj, ".rel.got");
797       if (s != NULL)
798         s->_raw_size = 0;
799     }
800
801   /* The check_relocs and adjust_dynamic_symbol entry points have
802      determined the sizes of the various dynamic sections.  Allocate
803      memory for them.  */
804   plt = false;
805   relocs = false;
806   reltext = false;
807   for (s = dynobj->sections; s != NULL; s = s->next)
808     {
809       const char *name;
810       boolean strip;
811
812       if ((s->flags & SEC_IN_MEMORY) == 0)
813         continue;
814
815       /* It's OK to base decisions on the section name, because none
816          of the dynobj section names depend upon the input files.  */
817       name = bfd_get_section_name (dynobj, s);
818
819       strip = false;
820
821       if (strcmp (name, ".plt") == 0)
822         {
823           if (s->_raw_size == 0)
824             {
825               /* Strip this section if we don't need it; see the
826                  comment below.  */
827               strip = true;
828             }
829           else
830             {
831               /* Remember whether there is a PLT.  */
832               plt = true;
833             }
834         }
835       else if (strncmp (name, ".rel", 4) == 0)
836         {
837           if (s->_raw_size == 0)
838             {
839               /* If we don't need this section, strip it from the
840                  output file.  This is mostly to handle .rel.bss and
841                  .rel.plt.  We must create both sections in
842                  create_dynamic_sections, because they must be created
843                  before the linker maps input sections to output
844                  sections.  The linker does that before
845                  adjust_dynamic_symbol is called, and it is that
846                  function which decides whether anything needs to go
847                  into these sections.  */
848               strip = true;
849             }
850           else
851             {
852               asection *target;
853
854               /* Remember whether there are any reloc sections other
855                  than .rel.plt.  */
856               if (strcmp (name, ".rel.plt") != 0)
857                 relocs = true;
858
859               /* If this relocation section applies to a read only
860                  section, then we probably need a DT_TEXTREL entry.  */
861               target = bfd_get_section_by_name (output_bfd, name + 4);
862               if (target != NULL
863                   && (target->flags & SEC_READONLY) != 0)
864                 reltext = true;
865
866               /* We use the reloc_count field as a counter if we need
867                  to copy relocs into the output file.  */
868               s->reloc_count = 0;
869             }
870         }
871       else if (strncmp (name, ".got", 4) != 0)
872         {
873           /* It's not one of our sections, so don't allocate space.  */
874           continue;
875         }
876
877       if (strip)
878         {
879           asection **spp;
880
881           for (spp = &s->output_section->owner->sections;
882                *spp != s->output_section;
883                spp = &(*spp)->next)
884             ;
885           *spp = s->output_section->next;
886           --s->output_section->owner->section_count;
887
888           continue;
889         }
890
891       /* Allocate memory for the section contents.  */
892       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
893       if (s->contents == NULL && s->_raw_size != 0)
894         {
895           bfd_set_error (bfd_error_no_memory);
896           return false;
897         }
898     }
899           
900   if (elf_hash_table (info)->dynamic_sections_created)
901     {
902       /* Add some entries to the .dynamic section.  We fill in the
903          values later, in elf_i386_finish_dynamic_sections, but we
904          must add the entries now so that we get the correct size for
905          the .dynamic section.  The DT_DEBUG entry is filled in by the
906          dynamic linker and used by the debugger.  */
907       if (! info->shared)
908         {
909           if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
910             return false;
911         }
912
913       if (plt)
914         {
915           if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
916               || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
917               || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL)
918               || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
919             return false;
920         }
921
922       if (relocs)
923         {
924           if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0)
925               || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0)
926               || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT,
927                                                 sizeof (Elf32_External_Rel)))
928             return false;
929         }
930
931       if (reltext)
932         {
933           if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
934             return false;
935         }
936     }
937
938   return true;
939 }
940
941 /* Relocate an i386 ELF section.  */
942
943 static boolean
944 elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
945                            contents, relocs, local_syms, local_sections)
946      bfd *output_bfd;
947      struct bfd_link_info *info;
948      bfd *input_bfd;
949      asection *input_section;
950      bfd_byte *contents;
951      Elf_Internal_Rela *relocs;
952      Elf_Internal_Sym *local_syms;
953      asection **local_sections;
954 {
955   bfd *dynobj;
956   Elf_Internal_Shdr *symtab_hdr;
957   struct elf_link_hash_entry **sym_hashes;
958   bfd_vma *local_got_offsets;
959   asection *sgot;
960   asection *splt;
961   asection *sreloc;
962   Elf_Internal_Rela *rel;
963   Elf_Internal_Rela *relend;
964
965   dynobj = elf_hash_table (info)->dynobj;
966   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
967   sym_hashes = elf_sym_hashes (input_bfd);
968   local_got_offsets = elf_local_got_offsets (input_bfd);
969
970   sgot = NULL;
971   splt = NULL;
972   sreloc = NULL;
973
974   rel = relocs;
975   relend = relocs + input_section->reloc_count;
976   for (; rel < relend; rel++)
977     {
978       int r_type;
979       reloc_howto_type *howto;
980       long r_symndx;
981       struct elf_link_hash_entry *h;
982       Elf_Internal_Sym *sym;
983       asection *sec;
984       bfd_vma relocation;
985       bfd_reloc_status_type r;
986       char *shared_name;
987
988       r_type = ELF32_R_TYPE (rel->r_info);
989       if (r_type < 0 || r_type >= (int) R_386_max)
990         {
991           bfd_set_error (bfd_error_bad_value);
992           return false;
993         }
994       howto = elf_howto_table + r_type;
995
996       r_symndx = ELF32_R_SYM (rel->r_info);
997
998       if (info->relocateable)
999         {
1000           /* This is a relocateable link.  We don't have to change
1001              anything, unless the reloc is against a section symbol,
1002              in which case we have to adjust according to where the
1003              section symbol winds up in the output section.  */
1004           if (r_symndx < symtab_hdr->sh_info)
1005             {
1006               sym = local_syms + r_symndx;
1007               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1008                 {
1009                   bfd_vma val;
1010
1011                   sec = local_sections[r_symndx];
1012                   val = bfd_get_32 (input_bfd, contents + rel->r_offset);
1013                   val += sec->output_offset + sym->st_value;
1014                   bfd_put_32 (input_bfd, val, contents + rel->r_offset);
1015                 }
1016             }
1017
1018           continue;
1019         }
1020
1021       /* This is a final link.  */
1022       h = NULL;
1023       sym = NULL;
1024       sec = NULL;
1025       shared_name = NULL;
1026
1027       if (r_symndx < symtab_hdr->sh_info)
1028         {
1029           sym = local_syms + r_symndx;
1030           sec = local_sections[r_symndx];
1031           relocation = (sec->output_section->vma
1032                         + sec->output_offset
1033                         + sym->st_value);
1034         }
1035       else
1036         {
1037           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1038           if (h->root.type == bfd_link_hash_defined
1039               || h->root.type == bfd_link_hash_defweak)
1040             {
1041               sec = h->root.u.def.section;
1042               if (r_type == R_386_GOTPC
1043                   || (r_type == R_386_PLT32
1044                       && h->plt_offset != (bfd_vma) -1)
1045                   || (r_type == R_386_GOT32
1046                       && elf_hash_table (info)->dynamic_sections_created)
1047                   || (info->shared
1048                       && (r_type == R_386_32
1049                           || r_type == R_386_PC32)
1050                       && (input_section->flags & SEC_ALLOC) != 0))
1051                 {
1052                   /* In these cases, we don't need the relocation
1053                      value.  We check specially because in some
1054                      obscure cases sec->output_section will be NULL.  */
1055                   relocation = 0;
1056                 }
1057               else
1058                 relocation = (h->root.u.def.value
1059                               + sec->output_section->vma
1060                               + sec->output_offset);
1061             }
1062           else if (h->root.type == bfd_link_hash_undefweak)
1063             relocation = 0;
1064           else if (info->shared)
1065             relocation = 0;
1066           else
1067             {
1068               if (! ((*info->callbacks->undefined_symbol)
1069                      (info, h->root.root.string, input_bfd,
1070                       input_section, rel->r_offset)))
1071                 return false;
1072               relocation = 0;
1073             }
1074         }
1075
1076       switch (r_type)
1077         {
1078         case R_386_GOT32:
1079           /* Relocation is to the entry for this symbol in the global
1080              offset table.  */
1081           if (sgot == NULL)
1082             {
1083               sgot = bfd_get_section_by_name (dynobj, ".got");
1084               BFD_ASSERT (sgot != NULL);
1085             }
1086
1087           if (h != NULL)
1088             {
1089               bfd_vma off;
1090
1091               off = h->got_offset;
1092               BFD_ASSERT (off != (bfd_vma) -1);
1093
1094               if (! elf_hash_table (info)->dynamic_sections_created)
1095                 {
1096                   /* This is actually a static link.  We must
1097                      initialize this entry in the global offset table.
1098                      Since the offset must always be a multiple of 4,
1099                      we use the least significant bit to record
1100                      whether we have initialized it already.
1101
1102                      When doing a dynamic link, we create a .rel.got
1103                      relocation entry to initialize the value.  This
1104                      is done in the finish_dynamic_symbol routine.  */
1105                   if ((off & 1) != 0)
1106                     off &= ~1;
1107                   else
1108                     {
1109                       bfd_put_32 (output_bfd, relocation,
1110                                   sgot->contents + off);
1111                       h->got_offset |= 1;
1112                     }
1113                 }
1114
1115               relocation = sgot->output_offset + off;
1116             }
1117           else
1118             {
1119               bfd_vma off;
1120
1121               BFD_ASSERT (local_got_offsets != NULL
1122                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
1123
1124               off = local_got_offsets[r_symndx];
1125
1126               /* The offset must always be a multiple of 4.  We use
1127                  the least significant bit to record whether we have
1128                  already generated the necessary reloc.  */
1129               if ((off & 1) != 0)
1130                 off &= ~1;
1131               else
1132                 {
1133                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1134
1135                   if (info->shared)
1136                     {
1137                       asection *srelgot;
1138                       Elf_Internal_Rel outrel;
1139
1140                       srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
1141                       BFD_ASSERT (srelgot != NULL);
1142
1143                       outrel.r_offset = (sgot->output_section->vma
1144                                          + sgot->output_offset
1145                                          + off);
1146                       outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
1147                       bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1148                                                 (((Elf32_External_Rel *)
1149                                                   srelgot->contents)
1150                                                  + srelgot->reloc_count));
1151                       ++srelgot->reloc_count;
1152                     }
1153
1154                   local_got_offsets[r_symndx] |= 1;
1155                 }
1156
1157               relocation = sgot->output_offset + off;
1158             }
1159
1160           break;
1161
1162         case R_386_GOTOFF:
1163           /* Relocation is relative to the start of the global offset
1164              table.  */
1165
1166           if (sgot == NULL)
1167             {
1168               sgot = bfd_get_section_by_name (dynobj, ".got");
1169               BFD_ASSERT (sgot != NULL);
1170             }
1171
1172           /* Note that sgot->output_offset is not involved in this
1173              calculation.  We always want the start of .got.  If we
1174              defined _GLOBAL_OFFSET_TABLE in a different way, as is
1175              permitted by the ABI, we might have to change this
1176              calculation.  */
1177           relocation -= sgot->output_section->vma;
1178
1179           break;
1180
1181         case R_386_GOTPC:
1182           /* Use global offset table as symbol value.  */
1183
1184           if (sgot == NULL)
1185             {
1186               sgot = bfd_get_section_by_name (dynobj, ".got");
1187               BFD_ASSERT (sgot != NULL);
1188             }
1189
1190           relocation = sgot->output_section->vma;
1191
1192           break;
1193
1194         case R_386_PLT32:
1195           /* Relocation is to the entry for this symbol in the
1196              procedure linkage table.  */
1197
1198           /* Resolve a PLT32 reloc again a local symbol directly,
1199              without using the procedure linkage table.  */
1200           if (h == NULL)
1201             break;
1202
1203           if (h->plt_offset == (bfd_vma) -1)
1204             {
1205               /* We didn't make a PLT entry for this symbol.  This
1206                  happens when statically linking PIC code.  */
1207               break;
1208             }
1209
1210           if (splt == NULL)
1211             {
1212               splt = bfd_get_section_by_name (dynobj, ".plt");
1213               BFD_ASSERT (splt != NULL);
1214             }
1215
1216           relocation = (splt->output_section->vma
1217                         + splt->output_offset
1218                         + h->plt_offset);
1219
1220           break;
1221
1222         case R_386_32:
1223         case R_386_PC32:
1224           if (info->shared
1225               && (input_section->flags & SEC_ALLOC) != 0)
1226             {
1227               Elf_Internal_Rel outrel;
1228
1229               /* When generating a shared object, these relocations
1230                  are copied into the output file to be resolved at run
1231                  time.  */
1232
1233               if (sreloc == NULL)
1234                 {
1235                   shared_name = (elf_string_from_elf_section
1236                           (input_bfd,
1237                            elf_elfheader (input_bfd)->e_shstrndx,
1238                            elf_section_data (input_section)->rel_hdr.sh_name));
1239                   if (shared_name == NULL)
1240                     return false;
1241
1242                   BFD_ASSERT (strncmp (shared_name, ".rel", 4) == 0
1243                               && strcmp (bfd_get_section_name (input_bfd,
1244                                                                input_section),
1245                                          shared_name + 4) == 0);
1246
1247                   sreloc = bfd_get_section_by_name (dynobj, shared_name);
1248                   BFD_ASSERT (sreloc != NULL);
1249                 }
1250
1251               outrel.r_offset = (rel->r_offset
1252                                  + input_section->output_section->vma
1253                                  + input_section->output_offset);
1254               if (r_type == R_386_PC32)
1255                 {
1256                   if (!h)
1257                     {
1258                       if (! ((*info->callbacks->undefined_symbol)
1259                              (info, shared_name ? shared_name : sec->name, input_bfd,
1260                               input_section, rel->r_offset)))
1261                         bfd_set_error (bfd_error_bad_value);
1262                       return false;
1263                     }
1264                   else {
1265                     BFD_ASSERT (h->dynindx != -1);
1266                     outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_PC32);
1267                   }
1268                 }
1269               else
1270                 {
1271                   if (h == NULL)
1272                     outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
1273                   else
1274                     {
1275                       BFD_ASSERT (h->dynindx != (bfd_vma) -1);
1276                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_32);
1277                     }
1278                 }
1279
1280               bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1281                                         (((Elf32_External_Rel *)
1282                                           sreloc->contents)
1283                                          + sreloc->reloc_count));
1284               ++sreloc->reloc_count;
1285
1286               /* If this reloc is against an external symbol, we do
1287                  not want to fiddle with the addend.  Otherwise, we
1288                  need to include the symbol value so that it becomes
1289                  an addend for the dynamic reloc.  */
1290               if (h != NULL)
1291                 continue;
1292             }
1293
1294           break;
1295
1296         default:
1297           break;
1298         }
1299
1300       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1301                                     contents, rel->r_offset,
1302                                     relocation, (bfd_vma) 0);
1303
1304       if (r != bfd_reloc_ok)
1305         {
1306           switch (r)
1307             {
1308             default:
1309             case bfd_reloc_outofrange:
1310               abort ();
1311             case bfd_reloc_overflow:
1312               {
1313                 const char *name;
1314
1315                 if (h != NULL)
1316                   name = h->root.root.string;
1317                 else
1318                   {
1319                     name = elf_string_from_elf_section (input_bfd,
1320                                                         symtab_hdr->sh_link,
1321                                                         sym->st_name);
1322                     if (name == NULL)
1323                       return false;
1324                     if (*name == '\0')
1325                       name = bfd_section_name (input_bfd, sec);
1326                   }
1327                 if (! ((*info->callbacks->reloc_overflow)
1328                        (info, name, howto->name, (bfd_vma) 0,
1329                         input_bfd, input_section, rel->r_offset)))
1330                   return false;
1331               }
1332               break;
1333             }
1334         }
1335     }
1336
1337   return true;
1338 }
1339
1340 /* Finish up dynamic symbol handling.  We set the contents of various
1341    dynamic sections here.  */
1342
1343 static boolean
1344 elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
1345      bfd *output_bfd;
1346      struct bfd_link_info *info;
1347      struct elf_link_hash_entry *h;
1348      Elf_Internal_Sym *sym;
1349 {
1350   bfd *dynobj;
1351
1352   dynobj = elf_hash_table (info)->dynobj;
1353
1354   if (h->plt_offset != (bfd_vma) -1)
1355     {
1356       asection *splt;
1357       asection *sgot;
1358       asection *srel;
1359       bfd_vma plt_index;
1360       bfd_vma got_offset;
1361       Elf_Internal_Rel rel;
1362
1363       /* This symbol has an entry in the procedure linkage table.  Set
1364          it up.  */
1365
1366       BFD_ASSERT (h->dynindx != -1);
1367
1368       splt = bfd_get_section_by_name (dynobj, ".plt");
1369       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1370       srel = bfd_get_section_by_name (dynobj, ".rel.plt");
1371       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1372
1373       /* Get the index in the procedure linkage table which
1374          corresponds to this symbol.  This is the index of this symbol
1375          in all the symbols for which we are making plt entries.  The
1376          first entry in the procedure linkage table is reserved.  */
1377       plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1;
1378
1379       /* Get the offset into the .got table of the entry that
1380          corresponds to this function.  Each .got entry is 4 bytes.
1381          The first three are reserved.  */
1382       got_offset = (plt_index + 3) * 4;
1383
1384       /* Fill in the entry in the procedure linkage table.  */
1385       if (! info->shared)
1386         {
1387           memcpy (splt->contents + h->plt_offset, elf_i386_plt_entry,
1388                   PLT_ENTRY_SIZE);
1389           bfd_put_32 (output_bfd,
1390                       (sgot->output_section->vma
1391                        + sgot->output_offset
1392                        + got_offset),
1393                       splt->contents + h->plt_offset + 2);
1394         }
1395       else
1396         {
1397           memcpy (splt->contents + h->plt_offset, elf_i386_pic_plt_entry,
1398                   PLT_ENTRY_SIZE);
1399           bfd_put_32 (output_bfd, got_offset,
1400                       splt->contents + h->plt_offset + 2);
1401         }
1402
1403       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
1404                   splt->contents + h->plt_offset + 7);
1405       bfd_put_32 (output_bfd, - (h->plt_offset + PLT_ENTRY_SIZE),
1406                   splt->contents + h->plt_offset + 12);
1407
1408       /* Fill in the entry in the global offset table.  */
1409       bfd_put_32 (output_bfd,
1410                   (splt->output_section->vma
1411                    + splt->output_offset
1412                    + h->plt_offset
1413                    + 6),
1414                   sgot->contents + got_offset);
1415
1416       /* Fill in the entry in the .rel.plt section.  */
1417       rel.r_offset = (sgot->output_section->vma
1418                       + sgot->output_offset
1419                       + got_offset);
1420       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
1421       bfd_elf32_swap_reloc_out (output_bfd, &rel,
1422                                 ((Elf32_External_Rel *) srel->contents
1423                                  + plt_index));
1424
1425       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1426         {
1427           /* Mark the symbol as undefined, rather than as defined in
1428              the .plt section.  Leave the value alone.  */
1429           sym->st_shndx = SHN_UNDEF;
1430         }
1431     }
1432
1433   if (h->got_offset != (bfd_vma) -1)
1434     {
1435       asection *sgot;
1436       asection *srel;
1437       Elf_Internal_Rel rel;
1438
1439       /* This symbol has an entry in the global offset table.  Set it
1440          up.  */
1441       
1442       BFD_ASSERT (h->dynindx != -1);
1443
1444       sgot = bfd_get_section_by_name (dynobj, ".got");
1445       srel = bfd_get_section_by_name (dynobj, ".rel.got");
1446       BFD_ASSERT (sgot != NULL && srel != NULL);
1447
1448       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
1449
1450       rel.r_offset = (sgot->output_section->vma
1451                       + sgot->output_offset
1452                       + h->got_offset);
1453       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
1454       bfd_elf32_swap_reloc_out (output_bfd, &rel,
1455                                 ((Elf32_External_Rel *) srel->contents
1456                                  + srel->reloc_count));
1457       ++srel->reloc_count;
1458     }
1459
1460   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1461     {
1462       asection *s;
1463       Elf_Internal_Rel rel;
1464
1465       /* This symbol needs a copy reloc.  Set it up.  */
1466
1467       BFD_ASSERT (h->dynindx != -1
1468                   && (h->root.type == bfd_link_hash_defined
1469                       || h->root.type == bfd_link_hash_defweak));
1470
1471       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1472                                    ".rel.bss");
1473       BFD_ASSERT (s != NULL);
1474
1475       rel.r_offset = (h->root.u.def.value
1476                       + h->root.u.def.section->output_section->vma
1477                       + h->root.u.def.section->output_offset);
1478       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
1479       bfd_elf32_swap_reloc_out (output_bfd, &rel,
1480                                 ((Elf32_External_Rel *) s->contents
1481                                  + s->reloc_count));
1482       ++s->reloc_count;
1483     }
1484
1485   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
1486   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1487       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1488     sym->st_shndx = SHN_ABS;
1489
1490   return true;
1491 }
1492
1493 /* Finish up the dynamic sections.  */
1494
1495 static boolean
1496 elf_i386_finish_dynamic_sections (output_bfd, info)
1497      bfd *output_bfd;
1498      struct bfd_link_info *info;
1499 {
1500   bfd *dynobj;
1501   asection *sgot;
1502   asection *sdyn;
1503
1504   dynobj = elf_hash_table (info)->dynobj;
1505
1506   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1507   BFD_ASSERT (sgot != NULL);
1508   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1509
1510   if (elf_hash_table (info)->dynamic_sections_created)
1511     {
1512       asection *splt;
1513       Elf32_External_Dyn *dyncon, *dynconend;
1514
1515       splt = bfd_get_section_by_name (dynobj, ".plt");
1516       BFD_ASSERT (splt != NULL && sdyn != NULL);
1517
1518       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1519       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1520       for (; dyncon < dynconend; dyncon++)
1521         {
1522           Elf_Internal_Dyn dyn;
1523           const char *name;
1524           asection *s;
1525
1526           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1527
1528           switch (dyn.d_tag)
1529             {
1530             default:
1531               break;
1532
1533             case DT_PLTGOT:
1534               name = ".got";
1535               goto get_vma;
1536             case DT_JMPREL:
1537               name = ".rel.plt";
1538             get_vma:
1539               s = bfd_get_section_by_name (output_bfd, name);
1540               BFD_ASSERT (s != NULL);
1541               dyn.d_un.d_ptr = s->vma;
1542               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1543               break;
1544
1545             case DT_PLTRELSZ:
1546               s = bfd_get_section_by_name (output_bfd, ".rel.plt");
1547               BFD_ASSERT (s != NULL);
1548               if (s->_cooked_size != 0)
1549                 dyn.d_un.d_val = s->_cooked_size;
1550               else
1551                 dyn.d_un.d_val = s->_raw_size;
1552               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1553               break;
1554
1555             case DT_RELSZ:
1556               /* My reading of the SVR4 ABI indicates that the
1557                  procedure linkage table relocs (DT_JMPREL) should be
1558                  included in the overall relocs (DT_REL).  This is
1559                  what Solaris does.  However, UnixWare can not handle
1560                  that case.  Therefore, we override the DT_RELSZ entry
1561                  here to make it not include the JMPREL relocs.  Since
1562                  the linker script arranges for .rel.plt to follow all
1563                  other relocation sections, we don't have to worry
1564                  about changing the DT_REL entry.  */
1565               s = bfd_get_section_by_name (output_bfd, ".rel.plt");
1566               if (s != NULL)
1567                 {
1568                   if (s->_cooked_size != 0)
1569                     dyn.d_un.d_val -= s->_cooked_size;
1570                   else
1571                     dyn.d_un.d_val -= s->_raw_size;
1572                 }
1573               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1574               break;
1575             }
1576         }
1577
1578       /* Fill in the first entry in the procedure linkage table.  */
1579       if (splt->_raw_size > 0)
1580         {
1581           if (info->shared)
1582             memcpy (splt->contents, elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE);
1583           else
1584             {
1585               memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE);
1586               bfd_put_32 (output_bfd,
1587                           sgot->output_section->vma + sgot->output_offset + 4,
1588                           splt->contents + 2);
1589               bfd_put_32 (output_bfd,
1590                           sgot->output_section->vma + sgot->output_offset + 8,
1591                           splt->contents + 8);
1592             }
1593         }
1594
1595       /* UnixWare sets the entsize of .plt to 4, although that doesn't
1596          really seem like the right value.  */
1597       elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
1598     }
1599
1600   /* Fill in the first three entries in the global offset table.  */
1601   if (sgot->_raw_size > 0)
1602     {
1603       if (sdyn == NULL)
1604         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1605       else
1606         bfd_put_32 (output_bfd,
1607                     sdyn->output_section->vma + sdyn->output_offset,
1608                     sgot->contents);
1609       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1610       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1611     }
1612
1613   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1614
1615   return true;
1616 }
1617
1618 #define TARGET_LITTLE_SYM               bfd_elf32_i386_vec
1619 #define TARGET_LITTLE_NAME              "elf32-i386"
1620 #define ELF_ARCH                        bfd_arch_i386
1621 #define ELF_MACHINE_CODE                EM_386
1622 #define elf_info_to_howto               elf_i386_info_to_howto
1623 #define elf_info_to_howto_rel           elf_i386_info_to_howto_rel
1624 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
1625 #define ELF_MAXPAGESIZE                 0x1000
1626 #define elf_backend_create_dynamic_sections \
1627                                         elf_i386_create_dynamic_sections
1628 #define elf_backend_check_relocs        elf_i386_check_relocs
1629 #define elf_backend_adjust_dynamic_symbol \
1630                                         elf_i386_adjust_dynamic_symbol
1631 #define elf_backend_size_dynamic_sections \
1632                                         elf_i386_size_dynamic_sections
1633 #define elf_backend_relocate_section    elf_i386_relocate_section
1634 #define elf_backend_finish_dynamic_symbol \
1635                                         elf_i386_finish_dynamic_symbol
1636 #define elf_backend_finish_dynamic_sections \
1637                                         elf_i386_finish_dynamic_sections
1638
1639 #include "elf32-target.h"