bfd/
[deliverable/binutils-gdb.git] / ld / pe-dll.c
index a308c94dbbf57b7a3927f078a80fb46ef3a8c7b6..ab743eed7c915478681ba23203f59394894f067d 100644 (file)
@@ -1,5 +1,5 @@
 /* Routines to help build PEI-format DLLs (Win32 etc)
-   Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
+   Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
    Free Software Foundation, Inc.
    Written by DJ Delorie <dj@cygnus.com>
 
@@ -20,8 +20,8 @@
    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
    02110-1301, USA.  */
 
-#include "bfd.h"
 #include "sysdep.h"
+#include "bfd.h"
 #include "bfdlink.h"
 #include "libiberty.h"
 #include "safe-ctype.h"
@@ -40,6 +40,7 @@
 #include "coff/internal.h"
 #include "../bfd/libcoff.h"
 #include "deffile.h"
+#include "pe-dll.h"
 
 #ifdef pe_use_x86_64
 
@@ -167,24 +168,24 @@ static int runtime_pseudo_relocs_created = 0;
 
 typedef struct
 {
-  char *name;
+  const char *name;
   int len;
 }
 autofilter_entry_type;
 
 typedef struct
 {
-  char *target_name;
-  char *object_target;
+  const char *target_name;
+  const char *object_target;
   unsigned int imagebase_reloc;
   int pe_arch;
   int bfd_arch;
   bfd_boolean underscored;
-  autofilter_entry_type* autofilter_symbollist; 
+  const autofilter_entry_type* autofilter_symbollist; 
 }
 pe_details_type;
 
-static autofilter_entry_type autofilter_symbollist_generic[] =
+static const autofilter_entry_type autofilter_symbollist_generic[] =
 {
   { STRING_COMMA_LEN (".text") },
   /* Entry point symbols.  */
@@ -197,7 +198,7 @@ static autofilter_entry_type autofilter_symbollist_generic[] =
   { STRING_COMMA_LEN (NULL) }
 };
 
-static autofilter_entry_type autofilter_symbollist_i386[] =
+static const autofilter_entry_type autofilter_symbollist_i386[] =
 {
   { STRING_COMMA_LEN (".text") },
   /* Entry point symbols, and entry hooks.  */
@@ -231,7 +232,7 @@ static autofilter_entry_type autofilter_symbollist_i386[] =
 #define PE_ARCH_arm_epoc 5
 #define PE_ARCH_arm_wince 6
 
-static pe_details_type pe_detail_list[] =
+static const pe_details_type pe_detail_list[] =
 {
   {
 #ifdef pe_use_x86_64
@@ -296,10 +297,10 @@ static pe_details_type pe_detail_list[] =
   { NULL, NULL, 0, 0, 0, FALSE, NULL }
 };
 
-static pe_details_type *pe_details;
+static const pe_details_type *pe_details;
 
 /* Do not specify library suffix explicitly, to allow for dllized versions.  */
-static autofilter_entry_type autofilter_liblist[] =
+static const autofilter_entry_type autofilter_liblist[] =
 {
   { STRING_COMMA_LEN ("libcegcc") },
   { STRING_COMMA_LEN ("libcygwin") },
@@ -314,7 +315,7 @@ static autofilter_entry_type autofilter_liblist[] =
   { STRING_COMMA_LEN (NULL) }
 };
 
-static autofilter_entry_type autofilter_objlist[] =
+static const autofilter_entry_type autofilter_objlist[] =
 {
   { STRING_COMMA_LEN ("crt0.o") },
   { STRING_COMMA_LEN ("crt1.o") },
@@ -329,7 +330,7 @@ static autofilter_entry_type autofilter_objlist[] =
   { STRING_COMMA_LEN (NULL) }
 };
 
-static autofilter_entry_type autofilter_symbolprefixlist[] =
+static const autofilter_entry_type autofilter_symbolprefixlist[] =
 {
   /* _imp_ is treated specially, as it is always underscored.  */
   /* { STRING_COMMA_LEN ("_imp_") },  */
@@ -343,7 +344,7 @@ static autofilter_entry_type autofilter_symbolprefixlist[] =
   { STRING_COMMA_LEN (NULL) }
 };
 
-static autofilter_entry_type autofilter_symbolsuffixlist[] =
+static const autofilter_entry_type autofilter_symbolsuffixlist[] =
 {
   { STRING_COMMA_LEN ("_iname") },
   { STRING_COMMA_LEN (NULL) }
@@ -449,7 +450,7 @@ pe_dll_add_excludes (const char *new_excludes, const int type)
 static bfd_boolean
 is_import (const char* n)
 {
-       return (CONST_STRNEQ (n, "__imp_"));
+  return (CONST_STRNEQ (n, "__imp_"));
 }
 
 /* abfd is a bfd containing n (or NULL)
@@ -460,7 +461,7 @@ auto_export (bfd *abfd, def_file *d, const char *n)
 {
   int i;
   struct exclude_list_struct *ex;
-  autofilter_entry_type *afptr;
+  const autofilter_entry_type *afptr;
   const char * libname = 0;
   if (abfd && abfd->my_archive)
     libname = lbasename (abfd->my_archive->filename);
@@ -619,10 +620,11 @@ process_def_file (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
 
                  /* We should not re-export imported stuff.  */
                  {
+                   char *name;
                    if (is_import (sn))
                          continue;
 
-                   char *name = xmalloc (strlen ("__imp_") + strlen (sn) + 1);
+                   name = xmalloc (strlen ("__imp_") + strlen (sn) + 1);
                    sprintf (name, "%s%s", "__imp_", sn);
 
                    blhe = bfd_link_hash_lookup (info->hash, name,
@@ -1797,6 +1799,10 @@ make_tail (bfd *parent)
   d7 = xmalloc (len);
   id7->contents = d7;
   strcpy ((char *) d7, dll_filename);
+  /* If len was odd, the above
+     strcpy leaves behind an undefined byte. That is harmless,
+     but we set it to 0 just so the binary dumps are pretty.  */
+  d7[len - 1] = 0;
 
   bfd_set_symtab (abfd, symtab, symptr);
 
@@ -1829,7 +1835,7 @@ make_tail (bfd *parent)
        .short          <hint>
        .asciz          "function" xlate? (add underscore, kill at)  */
 
-static unsigned char jmp_ix86_bytes[] =
+static const unsigned char jmp_ix86_bytes[] =
 {
   0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
 };
@@ -1841,7 +1847,7 @@ static unsigned char jmp_ix86_bytes[] =
        nop
        .dw     __imp_function   */
 
-static unsigned char jmp_sh_bytes[] =
+static const unsigned char jmp_sh_bytes[] =
 {
   0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00
 };
@@ -1852,13 +1858,13 @@ static unsigned char jmp_sh_bytes[] =
        jr      $t0
        nop                              */
 
-static unsigned char jmp_mips_bytes[] =
+static const unsigned char jmp_mips_bytes[] =
 {
   0x00, 0x00, 0x08, 0x3c,  0x00, 0x00, 0x08, 0x8d,
   0x08, 0x00, 0x00, 0x01,  0x00, 0x00, 0x00, 0x00
 };
 
-static unsigned char jmp_arm_bytes[] =
+static const unsigned char jmp_arm_bytes[] =
 {
   0x00, 0xc0, 0x9f, 0xe5,      /* ldr  ip, [pc] */
   0x00, 0xf0, 0x9c, 0xe5,      /* ldr  pc, [ip] */
@@ -1867,38 +1873,47 @@ static unsigned char jmp_arm_bytes[] =
 
 
 static bfd *
-make_one (def_file_export *exp, bfd *parent)
+make_one (def_file_export *exp, bfd *parent, bfd_boolean include_jmp_stub)
 {
   asection *tx, *id7, *id5, *id4, *id6;
   unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL;
   int len;
   char *oname;
   bfd *abfd;
-  unsigned char *jmp_bytes = NULL;
+  const unsigned char *jmp_bytes = NULL;
   int jmp_byte_count = 0;
 
-  switch (pe_details->pe_arch)
+  /* Include the jump stub section only if it is needed. A jump
+     stub is needed if the symbol being imported <sym> is a function
+     symbol and there is at least one undefined reference to that
+     symbol. In other words, if all the import references to <sym> are
+     explicitly through _declspec(dllimport) then the jump stub is not
+     needed.  */
+  if (include_jmp_stub)
     {
-    case PE_ARCH_i386:
-      jmp_bytes = jmp_ix86_bytes;
-      jmp_byte_count = sizeof (jmp_ix86_bytes);
-      break;
-    case PE_ARCH_sh:
-      jmp_bytes = jmp_sh_bytes;
-      jmp_byte_count = sizeof (jmp_sh_bytes);
-      break;
-    case PE_ARCH_mips:
-      jmp_bytes = jmp_mips_bytes;
-      jmp_byte_count = sizeof (jmp_mips_bytes);
-      break;
-    case PE_ARCH_arm:
-    case PE_ARCH_arm_epoc:
-    case PE_ARCH_arm_wince:
-      jmp_bytes = jmp_arm_bytes;
-      jmp_byte_count = sizeof (jmp_arm_bytes);
-      break;
-    default:
-      abort ();
+      switch (pe_details->pe_arch)
+       {
+       case PE_ARCH_i386:
+         jmp_bytes = jmp_ix86_bytes;
+         jmp_byte_count = sizeof (jmp_ix86_bytes);
+         break;
+       case PE_ARCH_sh:
+         jmp_bytes = jmp_sh_bytes;
+         jmp_byte_count = sizeof (jmp_sh_bytes);
+         break;
+       case PE_ARCH_mips:
+         jmp_bytes = jmp_mips_bytes;
+         jmp_byte_count = sizeof (jmp_mips_bytes);
+         break;
+       case PE_ARCH_arm:
+       case PE_ARCH_arm_epoc:
+       case PE_ARCH_arm_wince:
+         jmp_bytes = jmp_arm_bytes;
+         jmp_byte_count = sizeof (jmp_arm_bytes);
+         break;
+       default:
+         abort ();
+       }
     }
 
   oname = xmalloc (20);
@@ -1924,7 +1939,7 @@ make_one (def_file_export *exp, bfd *parent)
     {
       quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC,
                    BSF_GLOBAL, 0);
-      if (! exp->flag_data)
+      if (include_jmp_stub)
        quick_symbol (abfd, "", exp->internal_name, "", tx, BSF_GLOBAL, 0);
       quick_symbol (abfd, "__imp_", exp->internal_name, "", id5,
                    BSF_GLOBAL, 0);
@@ -1935,7 +1950,7 @@ make_one (def_file_export *exp, bfd *parent)
     {
       quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC,
                    BSF_GLOBAL, 0);
-      if (! exp->flag_data)
+      if (include_jmp_stub)
        quick_symbol (abfd, U (""), exp->internal_name, "", tx,
                      BSF_GLOBAL, 0);
       quick_symbol (abfd, "__imp_", U (""), exp->internal_name, id5,
@@ -1950,7 +1965,7 @@ make_one (def_file_export *exp, bfd *parent)
     quick_symbol (abfd, U ("__imp_"), exp->internal_name, "", id5,
                  BSF_GLOBAL, 0);
 
-  if (! exp->flag_data)
+  if (include_jmp_stub)
     {
       bfd_set_section_size (abfd, tx, jmp_byte_count);
       td = xmalloc (jmp_byte_count);
@@ -1960,7 +1975,11 @@ make_one (def_file_export *exp, bfd *parent)
       switch (pe_details->pe_arch)
        {
        case PE_ARCH_i386:
-         quick_reloc (abfd, 2, BFD_RELOC_32, 2);
+#ifdef pe_use_x86_64
+         quick_reloc (abfd, 2, BFD_RELOC_32_PCREL, 2);
+#else
+          quick_reloc (abfd, 2, BFD_RELOC_32, 2);
+#endif
          break;
        case PE_ARCH_sh:
          quick_reloc (abfd, 8, BFD_RELOC_32, 2);
@@ -1980,6 +1999,8 @@ make_one (def_file_export *exp, bfd *parent)
        }
       save_relocs (tx);
     }
+  else
+    bfd_set_section_size (abfd, tx, 0);
 
   bfd_set_section_size (abfd, id7, 4);
   d7 = xmalloc (4);
@@ -2029,7 +2050,8 @@ make_one (def_file_export *exp, bfd *parent)
     }
   else
     {
-      len = strlen (exp->name) + 3;
+      /* { short, asciz }  */
+      len = 2 + strlen (exp->name) + 1;
       if (len & 1)
        len++;
       bfd_set_section_size (abfd, id6, len);
@@ -2043,7 +2065,8 @@ make_one (def_file_export *exp, bfd *parent)
 
   bfd_set_symtab (abfd, symtab, symptr);
 
-  bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
+  if (include_jmp_stub)
+    bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
   bfd_set_section_contents (abfd, id7, d7, 0, 4);
   bfd_set_section_contents (abfd, id5, d5, 0, PE_IDATA5_SIZE);
   bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE);
@@ -2080,16 +2103,17 @@ make_singleton_name_thunk (const char *import, bfd *parent)
   quick_symbol (abfd, U ("_nm_thnk_"), import, "", id4, BSF_GLOBAL, 0);
   quick_symbol (abfd, U ("_nm_"), import, "", UNDSEC, BSF_GLOBAL, 0);
 
-  bfd_set_section_size (abfd, id4, PE_IDATA4_SIZE);
-  d4 = xmalloc (PE_IDATA4_SIZE);
+  /* We need space for the real thunk and for the null terminator.  */
+  bfd_set_section_size (abfd, id4, PE_IDATA4_SIZE * 2);
+  d4 = xmalloc (PE_IDATA4_SIZE * 2);
   id4->contents = d4;
-  memset (d4, 0, PE_IDATA4_SIZE);
+  memset (d4, 0, PE_IDATA4_SIZE * 2);
   quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
   save_relocs (id4);
 
   bfd_set_symtab (abfd, symtab, symptr);
 
-  bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE);
+  bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE * 2);
 
   bfd_make_readable (abfd);
   return abfd;
@@ -2390,8 +2414,9 @@ pe_dll_generate_implib (def_file *def, const char *impfilename)
       if (pe_def_file->exports[i].flag_private)
        continue;
       def->exports[i].internal_name = def->exports[i].name;
-      n = make_one (def->exports + i, outarch);
-      n->next = head;
+      n = make_one (def->exports + i, outarch,
+                   ! (def->exports + i)->flag_data);
+      n->archive_next = head;
       head = n;
       def->exports[i].internal_name = internal;
     }
@@ -2402,8 +2427,8 @@ pe_dll_generate_implib (def_file *def, const char *impfilename)
     return;
 
   /* Now stick them all into the archive.  */
-  ar_head->next = head;
-  ar_tail->next = ar_head;
+  ar_head->archive_next = head;
+  ar_tail->archive_next = ar_head;
   head = ar_tail;
 
   if (! bfd_set_archive_head (outarch, head))
@@ -2414,7 +2439,7 @@ pe_dll_generate_implib (def_file *def, const char *impfilename)
 
   while (head != NULL)
     {
-      bfd *n = head->next;
+      bfd *n = head->archive_next;
       bfd_close (head);
       head = n;
     }
@@ -2466,6 +2491,7 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *link_info)
            /* See if we need this import.  */
            size_t len = strlen (pe_def_file->imports[i].internal_name);
            char *name = xmalloc (len + 2 + 6);
+           bfd_boolean include_jmp_stub = FALSE;
 
            if (lead_at)
              sprintf (name, "%s",
@@ -2477,6 +2503,8 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *link_info)
            blhe = bfd_link_hash_lookup (link_info->hash, name,
                                         FALSE, FALSE, FALSE);
 
+           /* Include the jump stub for <sym> only if the <sym>
+              is undefined.  */
            if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
              {
                if (lead_at)
@@ -2489,6 +2517,9 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *link_info)
                blhe = bfd_link_hash_lookup (link_info->hash, name,
                                             FALSE, FALSE, FALSE);
              }
+           else
+             include_jmp_stub = TRUE;
+
            free (name);
 
            if (blhe && blhe->type == bfd_link_hash_undefined)
@@ -2509,7 +2540,7 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *link_info)
                exp.flag_constant = 0;
                exp.flag_data = pe_def_file->imports[i].data;
                exp.flag_noname = exp.name ? 0 : 1;
-               one = make_one (&exp, output_bfd);
+               one = make_one (&exp, output_bfd, (! exp.flag_data) && include_jmp_stub);
                add_bfd_to_link (one, one->filename, link_info);
              }
          }
@@ -2823,3 +2854,11 @@ pe_exe_fill_sections (bfd *abfd, struct bfd_link_info *info)
     }
   reloc_s->contents = reloc_d;
 }
+
+bfd_boolean
+pe_bfd_is_dll (bfd *abfd)
+{
+  return (bfd_get_format (abfd) == bfd_object
+          && obj_pe (abfd)
+          && pe_data (abfd)->dll);
+}
This page took 0.030202 seconds and 4 git commands to generate.