daily update
[binutils-gdb.git] / ld / pe-dll.c
index d9e40783e35588dfdf8ac9ad27746d4a3e98e113..1430754842a6515abd282b4a17e9503fa539cd5e 100644 (file)
@@ -1,5 +1,5 @@
 /* Routines to help build PEI-format DLLs (Win32 etc)
-   Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+   Copyright 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
    Written by DJ Delorie <dj@cygnus.com>
 
    This file is part of GLD, the Gnu Linker.
@@ -59,7 +59,7 @@
     code modifications).
 
     2. This is done completely in bounds of the PE specification (to be fair,
-    there's a place where it pokes nose out of, but in practise it works).
+    there's a place where it pokes nose out of, but in practice it works).
     So, resulting module can be used with any other PE compiler/linker.
 
     3. Auto-import is fully compatible with standard import method and they
@@ -74,7 +74,7 @@
 
     The obvious and only way to get rid of dllimport insanity is to make client
     access variable directly in the DLL, bypassing extra dereference. I.e.,
-    whenever client contains someting like
+    whenever client contains something like
 
     mov dll_var,%eax,
 
@@ -82,7 +82,7 @@
     DLL. The aim is to make OS loader do so, and than make ld help with that.
     Import section of PE made following way: there's a vector of structures
     each describing imports from particular DLL. Each such structure points
-    to two other parellel vectors: one holding imported names, and one which
+    to two other parallel vectors: one holding imported names, and one which
     will hold address of corresponding imported name. So, the solution is
     de-vectorize these structures, making import locations be sparse and
     pointing directly into code. Before continuing, it is worth a note that,
     above: PE specification rambles that name vector (OriginalFirstThunk)
     should run in parallel with addresses vector (FirstThunk), i.e. that they
     should have same number of elements and terminated with zero. We violate
-    this, since FirstThunk points directly into machine code. But in practise,
+    this, since FirstThunk points directly into machine code. But in practice,
     OS loader implemented the sane way: it goes thru OriginalFirstThunk and
     puts addresses to FirstThunk, not something else. It once again should be
     noted that dll and symbol name structures are reused across fixup entries
     in windows9x kernel32.dll, so if you use it, you have two
     IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is whether
     referencing the same PE structures several times is valid. The answer is why
-    not, prohibitting that (detecting violation) would require more work on
+    not, prohibiting that (detecting violation) would require more work on
     behalf of loader than not doing it.
 
     See also: ld/emultempl/pe.em.  */
@@ -199,7 +199,7 @@ static pe_details_type pe_detail_list[] =
     11 /* ARM_RVA32 */,
     PE_ARCH_arm,
     bfd_arch_arm,
-    0
+    1
   },
   {
     "epoc-pei-arm-little",
@@ -223,6 +223,9 @@ static autofilter_entry_type autofilter_symbollist[] =
   { "_cygwin_crt0_common@8", 21 },
   { "_cygwin_noncygwin_dll_entry@12", 30 },
   { "impure_ptr", 10 },
+  { "_pei386_runtime_relocator", 25 },
+  { "do_pseudo_reloc", 15 },
+  { "cygwin_crt0", 11 },
   { NULL, 0 }
 };
 
@@ -561,6 +564,12 @@ process_def_file (abfd, info)
        }
     }
 
+  /* If we are not building a DLL, when there are no exports
+     we do not build an export table at all.  */
+  if (!pe_dll_export_everything && pe_def_file->num_exports == 0
+      && !(info->shared))
+    return;
+
   /* Now, maybe export everything else the default way.  */
   if (pe_dll_export_everything || pe_def_file->num_exports == 0)
     {
@@ -590,7 +599,7 @@ process_def_file (abfd, info)
                    sprintf (name, "%s%s", U("_imp_"), sn);
 
                    blhe = bfd_link_hash_lookup (info->hash, name,
-                                                false, false, false);
+                                                FALSE, FALSE, FALSE);
                    free (name);
 
                    if (blhe && blhe->type == bfd_link_hash_defined)
@@ -721,7 +730,7 @@ process_def_file (abfd, info)
 
       blhe = bfd_link_hash_lookup (info->hash,
                                   name,
-                                  false, false, true);
+                                  FALSE, FALSE, TRUE);
 
       if (blhe
          && (blhe->type == bfd_link_hash_defined
@@ -936,7 +945,7 @@ fill_exported_offsets (abfd, info)
 
       blhe = bfd_link_hash_lookup (info->hash,
                                   name,
-                                  false, false, true);
+                                  FALSE, FALSE, TRUE);
 
       if (blhe && (blhe->type == bfd_link_hash_defined))
        exported_symbol_offsets[i] = blhe->u.def.value;
@@ -1822,7 +1831,7 @@ make_one (exp, parent)
   id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
   id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
   id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2);
+
   if  (*exp->internal_name == '@')
     {
       if (! exp->flag_data)
@@ -2015,8 +2024,8 @@ make_import_fixup_mark (rel)
      overflowing this buffer...  */
     {
       free (fixup_name);
-      /* New buffer size is length of symbol, plus 25, but then
-        rounded up to the nearest multiple of 128.  */
+      /* New buffer size is length of symbol, plus 25, but
+        then rounded up to the nearest multiple of 128.  */
       buffer_len = ((strlen (sym->name) + 25) + 127) & ~127;
       fixup_name = (char *) xmalloc (buffer_len);
     }
@@ -2026,7 +2035,7 @@ make_import_fixup_mark (rel)
   bh = NULL;
   bfd_coff_link_add_one_symbol (&link_info, abfd, fixup_name, BSF_GLOBAL,
                                current_sec, /* sym->section, */
-                               rel->address, NULL, true, false, &bh);
+                               rel->address, NULL, TRUE, FALSE, &bh);
 
   if (0)
     {
@@ -2150,7 +2159,7 @@ make_runtime_pseudo_reloc (name, fixup_name, addend, parent)
 }
 
 /*     .section        .rdata
-       .rva            __pei386_runtime_relocator */
+       .rva            __pei386_runtime_relocator  */
 
 static bfd *
 pe_create_runtime_relocator_reference (parent)
@@ -2216,7 +2225,7 @@ pe_create_import_fixup (rel, s, addend)
       add_bfd_to_link (b, b->filename, &link_info);
 
       /* If we ever use autoimport, we have to cast text section writable.  */
-      config.text_read_only = false;
+      config.text_read_only = FALSE;
     }
 
   if (addend == 0 || link_info.pei386_runtime_pseudo_reloc)
@@ -2244,7 +2253,7 @@ pe_create_import_fixup (rel, s, addend)
              add_bfd_to_link (b, b->filename, &link_info);
            }
          runtime_pseudo_relocs_created++;
-       } 
+       }
       else
        {
          einfo (_("%C: variable '%T' can't be auto-imported. Please read the documentation for ld's --enable-auto-import for details.\n"),
@@ -2376,7 +2385,7 @@ pe_process_import_defs (output_bfd, link_info)
          {
            def_file_export exp;
            struct bfd_link_hash_entry *blhe;
-           int lead_at = (*pe_def_file->imports[i].internal_name == '@');  
+           int lead_at = (*pe_def_file->imports[i].internal_name == '@');
            /* See if we need this import.  */
            char *name = (char *) xmalloc (strlen (pe_def_file->imports[i].internal_name) + 2 + 6);
 
@@ -2386,7 +2395,7 @@ pe_process_import_defs (output_bfd, link_info)
              sprintf (name, "%s%s",U (""), pe_def_file->imports[i].internal_name);
 
            blhe = bfd_link_hash_lookup (link_info->hash, name,
-                                        false, false, false);
+                                        FALSE, FALSE, FALSE);
 
            if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
              {
@@ -2398,7 +2407,7 @@ pe_process_import_defs (output_bfd, link_info)
                           pe_def_file->imports[i].internal_name);
 
                blhe = bfd_link_hash_lookup (link_info->hash, name,
-                                            false, false, false);
+                                            FALSE, FALSE, FALSE);
              }
            free (name);
 
@@ -2418,7 +2427,7 @@ pe_process_import_defs (output_bfd, link_info)
                exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0;
                exp.flag_private = 0;
                exp.flag_constant = 0;
-               exp.flag_data = 0;
+               exp.flag_data = pe_def_file->imports[i].data;
                exp.flag_noname = exp.name ? 0 : 1;
                one = make_one (&exp, output_bfd);
                add_bfd_to_link (one, one->filename, link_info);
@@ -2435,8 +2444,8 @@ pe_process_import_defs (output_bfd, link_info)
 }
 
 /* We were handed a *.DLL file.  Parse it and turn it into a set of
-   IMPORTS directives in the def file.  Return true if the file was
-   handled, false if not.  */
+   IMPORTS directives in the def file.  Return TRUE if the file was
+   handled, FALSE if not.  */
 
 static unsigned int
 pe_get16 (abfd, where)
@@ -2484,16 +2493,23 @@ pe_as32 (ptr)
   return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
 }
 
-boolean
+bfd_boolean
 pe_implied_import_dll (filename)
      const char *filename;
 {
   bfd *dll;
   unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
   unsigned long export_rva, export_size, nsections, secptr, expptr;
+  unsigned long exp_funcbase;
   unsigned char *expdata, *erva;
   unsigned long name_rvas, ordinals, nexp, ordbase;
   const char *dll_name;
+  /* Initialization with start > end guarantees that is_data
+     will not be set by mistake, and avoids compiler warning.  */
+  unsigned long data_start = 1;
+  unsigned long data_end   = 0;
+  unsigned long bss_start  = 1;
+  unsigned long bss_end    = 0;
 
   /* No, I can't use bfd here.  kernel32.dll puts its export table in
      the middle of the .rdata section.  */
@@ -2501,27 +2517,23 @@ pe_implied_import_dll (filename)
   if (!dll)
     {
       einfo ("%Xopen %s: %s\n", filename, bfd_errmsg (bfd_get_error ()));
-      return false;
+      return FALSE;
     }
 
   /* PEI dlls seem to be bfd_objects.  */
   if (!bfd_check_format (dll, bfd_object))
     {
       einfo ("%X%s: this doesn't appear to be a DLL\n", filename);
-      return false;
+      return FALSE;
     }
 
-  dll_name = filename;
-  for (i = 0; filename[i]; i++)
-    if (filename[i] == '/' || filename[i] == '\\' || filename[i] == ':')
-      dll_name = filename + i + 1;
-
+  /* Get pe_header, optional header and numbers of export entries.  */
   pe_header_offset = pe_get32 (dll, 0x3c);
   opthdr_ofs = pe_header_offset + 4 + 20;
   num_entries = pe_get32 (dll, opthdr_ofs + 92);
 
   if (num_entries < 1) /* No exports.  */
-    return false;
+    return FALSE;
 
   export_rva = pe_get32 (dll, opthdr_ofs + 96);
   export_size = pe_get32 (dll, opthdr_ofs + 100);
@@ -2530,6 +2542,7 @@ pe_implied_import_dll (filename)
            pe_get16 (dll, pe_header_offset + 4 + 16));
   expptr = 0;
 
+  /* Get the rva and size of the export section.  */ 
   for (i = 0; i < nsections; i++)
     {
       char sname[8];
@@ -2550,6 +2563,40 @@ pe_implied_import_dll (filename)
        }
     }
 
+  /* Scan sections and store the base and size of the
+     data and bss segments in data/base_start/end.  */ 
+  for (i = 0; i < nsections; i++)
+    {
+      unsigned long secptr1 = secptr + 40 * i;
+      unsigned long vsize = pe_get32 (dll, secptr1 + 8);
+      unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
+      unsigned long flags = pe_get32 (dll, secptr1 + 36);
+      char sec_name[9];
+
+      sec_name[8] = '\0';
+      bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET);
+      bfd_bread (sec_name, (bfd_size_type) 8, dll);
+
+      if (strcmp(sec_name,".data") == 0)
+       {
+         data_start = vaddr;
+         data_end = vaddr + vsize;
+
+         if (pe_dll_extra_pe_debug)
+           printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
+                   __FUNCTION__, sec_name, vaddr, vaddr + vsize, flags);
+        }
+      else if (strcmp (sec_name,".bss") == 0)
+       {
+         bss_start = vaddr;
+         bss_end = vaddr + vsize;
+
+         if (pe_dll_extra_pe_debug)
+           printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
+                   __FUNCTION__, sec_name, vaddr, vaddr + vsize, flags);
+       }
+    }
+
   expdata = (unsigned char *) xmalloc (export_size);
   bfd_seek (dll, (file_ptr) expptr, SEEK_SET);
   bfd_bread (expdata, (bfd_size_type) export_size, dll);
@@ -2562,17 +2609,54 @@ pe_implied_import_dll (filename)
   name_rvas = pe_as32 (expdata + 32);
   ordinals = pe_as32 (expdata + 36);
   ordbase = pe_as32 (expdata + 16);
+  exp_funcbase = pe_as32 (expdata + 28);
+
+  /* Use internal dll name instead of filename
+     to enable symbolic dll linking.  */
+  dll_name = pe_as32 (expdata + 12) + erva;
 
+  /* Check to see if the dll has already been added to
+     the definition list and if so return without error.
+     This avoids multiple symbol definitions.  */
+  if (def_get_module (pe_def_file, dll_name))
+    {
+      if (pe_dll_extra_pe_debug)
+       printf ("%s is already loaded\n", dll_name);
+      return TRUE;
+    }
+
+  /* Iterate through the list of symbols.  */
   for (i = 0; i < nexp; i++)
     {
+      /* Pointer to the names vector.  */
       unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
       def_file_import *imp;
-
-      imp = def_file_add_import (pe_def_file, erva + name_rva, dll_name,
-                                i, 0);
+      /* Pointer to the function address vector.  */ 
+      unsigned long func_rva = pe_as32 (erva + exp_funcbase + i * 4);
+      int is_data = 0;
+
+      /* Skip unwanted symbols, which are
+        exported in buggy auto-import releases.  */
+      if (strncmp (erva + name_rva, "_nm_", 4) != 0)
+       {
+         /* is_data is true if the address is in the data or bss segment.  */
+         is_data =
+           (func_rva >= data_start && func_rva < data_end)
+           || (func_rva >= bss_start && func_rva < bss_end);
+
+         imp = def_file_add_import (pe_def_file, erva + name_rva,
+                                    dll_name, i, 0);
+         /* Mark symbol type.  */
+         imp->data = is_data;
+         if (pe_dll_extra_pe_debug)
+           printf ("%s dll-name: %s sym: %s addr: 0x%lx %s\n",
+                   __FUNCTION__, dll_name, erva + name_rva,
+                   func_rva, is_data ? "(data)" : "");
+       }
     }
 
-  return true;
+  return TRUE;
 }
 
 /* These are the main functions, called from the emulation.  The first
@@ -2588,6 +2672,9 @@ pe_dll_build_sections (abfd, info)
   pe_dll_id_target (bfd_get_target (abfd));
   process_def_file (abfd, info);
 
+  if (pe_def_file->num_exports == 0 && !(info->shared))
+    return;
+
   generate_edata (abfd, info);
   build_filler_bfd (1);
 }
@@ -2616,7 +2703,7 @@ pe_dll_fill_sections (abfd, info)
 
       /* Resize the sections.  */
       lang_size_sections (stat_ptr->head, abs_output_section,
-                         &stat_ptr->head, 0, (bfd_vma) 0, NULL);
+                         &stat_ptr->head, 0, (bfd_vma) 0, NULL, TRUE);
 
       /* Redo special stuff.  */
       ldemul_after_allocation ();
@@ -2629,7 +2716,8 @@ pe_dll_fill_sections (abfd, info)
 
   fill_edata (abfd, info);
 
-  pe_data (abfd)->dll = 1;
+  if (info->shared)
+    pe_data (abfd)->dll = 1;
 
   edata_s->contents = edata_d;
   reloc_s->contents = reloc_d;
@@ -2650,7 +2738,7 @@ pe_exe_fill_sections (abfd, info)
 
       /* Resize the sections.  */
       lang_size_sections (stat_ptr->head, abs_output_section,
-                         &stat_ptr->head, 0, (bfd_vma) 0, NULL);
+                         &stat_ptr->head, 0, (bfd_vma) 0, NULL, TRUE);
 
       /* Redo special stuff.  */
       ldemul_after_allocation ();