public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Mumit Khan <khan@xraylith.wisc.EDU>
To: binutils@sourceware.cygnus.com
Subject: (patch) x86-pe ld DLL auto-image-base options
Date: Sun, 29 Aug 1999 17:02:00 -0000	[thread overview]
Message-ID: <199908300001.TAA02985@mercury.xraylith.wisc.edu> (raw)

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;
 }
 \f
+
+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

                 reply	other threads:[~1999-08-29 17:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=199908300001.TAA02985@mercury.xraylith.wisc.edu \
    --to=khan@xraylith.wisc.edu \
    --cc=binutils@sourceware.cygnus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).