This is the mail archive of the binutils@sourceware.cygnus.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

(patch) x86-pe ld DLL auto-image-base options


The following patch adds options to let PE ld automatically choose
an "appropriate" image base for DLLs if not specified by user. Currently,
if the user does not specify one, the default is used; this pretty
much kills the performance (and memory usage) when your application
loads quite a few DLLs having the same base address (due to fixups
and the resultant unsharability). This change will also help 
libtool-style configuration programs that use gcc/ld --shared to create 
dynamically loaded modules.

I've set the default to be off, but it's probably better to turn it
on by default, which is what dllwrap does. This code is in fact 
just a cut-and-paste from ../binutils/dllwrap.c.

Sun Aug 29 18:45:58 1999  Mumit Khan  <khan@xraylith.wisc.edu>

	* emultempl/pe.em (pe_enable_auto_image_base): New variable. 
	(longopts): New --{enable,disable}-auto-image-base options.
	(gld_${EMULATION_NAME}_list_options): Document. 
	(gld_${EMULATION_NAME}_parse): Handle.
	(strhash): New static function.
	(compute_dll_image_base): New static function. 
	(gld_${EMULATION_NAME}_set_symbols): Use.

Index: emultempl/pe.em
===================================================================
RCS file: /homes/khan/src/CVSROOT/binutils/ld/emultempl/pe.em,v
retrieving revision 1.1.1.1
diff -u -3 -p -r1.1.1.1 pe.em
--- pe.em	1999/08/14 17:27:05	1.1.1.1
+++ pe.em	1999/08/29 23:43:57
@@ -89,6 +89,7 @@ extern int pe_dll_kill_ats;
 extern int pe_dll_stdcall_aliases;
 static int pe_enable_stdcall_fixup = -1; /* 0=disable 1=enable */
 static char *pe_implib_filename = 0;
+static int pe_enable_auto_image_base = 0; 
 
 extern const char *output_filename;
 
@@ -129,6 +130,8 @@ gld_${EMULATION_NAME}_before_parse()
 #define OPTION_DISABLE_STDCALL_FIXUP	(OPTION_ENABLE_STDCALL_FIXUP + 1)
 #define OPTION_IMPLIB_FILENAME		(OPTION_DISABLE_STDCALL_FIXUP + 1)
 #define OPTION_THUMB_ENTRY		(OPTION_IMPLIB_FILENAME + 1)
+#define OPTION_ENABLE_AUTO_IMAGE_BASE	(OPTION_THUMB_ENTRY + 1)
+#define OPTION_DISABLE_AUTO_IMAGE_BASE	(OPTION_ENABLE_AUTO_IMAGE_BASE + 1)
 
 static struct option longopts[] =
 {
@@ -161,6 +164,8 @@ static struct option longopts[] =
   {"enable-stdcall-fixup", no_argument, NULL, OPTION_ENABLE_STDCALL_FIXUP},
   {"disable-stdcall-fixup", no_argument, NULL, OPTION_DISABLE_STDCALL_FIXUP},
   {"out-implib", required_argument, NULL, OPTION_IMPLIB_FILENAME},
+  {"enable-auto-image-base", no_argument, NULL, OPTION_ENABLE_AUTO_IMAGE_BASE},
+  {"disable-auto-image-base", no_argument, NULL, OPTION_DISABLE_AUTO_IMAGE_BASE},
 #endif
   {NULL, no_argument, NULL, 0}
 };
@@ -233,6 +238,9 @@ gld_${EMULATION_NAME}_list_options (file
   fprintf (file, _("  --kill-at                          Remove @nn from exported symbols\n"));
   fprintf (file, _("  --out-implib <file>                Generate import library\n"));
   fprintf (file, _("  --output-def <file>                Generate a .DEF file for the built DLL\n"));
+  fprintf (file, _("  --enable-auto-image-base           Automatically choose image base for DLLs\n"));
+  fprintf (file, _("                                       unless user specifies one\n"));
+  fprintf (file, _("  --disable-auto-image-base          Do not automatically choose image base.\n"));
 #endif
 }
 
@@ -460,11 +468,49 @@ gld_${EMULATION_NAME}_parse_args(argc, a
     case OPTION_IMPLIB_FILENAME:
       pe_implib_filename = xstrdup (optarg);
       break;
+    case OPTION_ENABLE_AUTO_IMAGE_BASE:
+      pe_enable_auto_image_base = 1;
+      break;
+    case OPTION_DISABLE_AUTO_IMAGE_BASE:
+      pe_enable_auto_image_base = 0;
+      break;
 #endif
     }
   return 1;
 }
 
+
+static unsigned long 
+strhash (const char *str)
+{
+  const unsigned char *s;
+  unsigned long hash;
+  unsigned int c;
+  unsigned int len;
+
+  hash = 0;
+  len = 0;
+  s = (const unsigned char *) str;
+  while ((c = *s++) != '\0')
+    {
+      hash += c + (c << 17);
+      hash ^= hash >> 2;
+      ++len;
+    }
+  hash += len + (len << 17);
+  hash ^= hash >> 2;
+
+  return hash;
+}
+
+/* Use the output file to create a image base for relocatable DLLs. */
+static unsigned long
+compute_dll_image_base (const char *ofile)
+{
+  unsigned long hash = strhash (ofile);
+  return 0x60000000 | ((hash << 16) & 0xFFC0000);
+}
+
 /* Assign values to the special symbols before the linker script is
    read.  */
 
@@ -481,7 +527,8 @@ gld_${EMULATION_NAME}_set_symbols ()
       if (link_info.relocateable)
 	init[IMAGEBASEOFF].value = 0;
       else if (init[DLLOFF].value || link_info.shared)
-	init[IMAGEBASEOFF].value = NT_DLL_IMAGE_BASE;
+	init[IMAGEBASEOFF].value = (pe_enable_auto_image_base) ?  
+	  compute_dll_image_base (output_filename) : NT_DLL_IMAGE_BASE;
       else
 	init[IMAGEBASEOFF].value = NT_EXE_IMAGE_BASE;
     }

Regards,
Mumit


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]