delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/01/01/13:50:15

From: <ams AT ludd DOT luth DOT se>
Message-Id: <200301011849.h01InxE21061@speedy.ludd.luth.se>
Subject: Re: stubify
In-Reply-To: <200212310103.gBV13jt27469@speedy.ludd.luth.se> "from ams@ludd.luth.se
at Dec 31, 2002 02:03:45 am"
To: djgpp-workers AT delorie DOT com
Date: Wed, 1 Jan 2003 19:49:58 +0100 (CET)
X-Mailer: ELM [version 2.4ME+ PL78 (25)]
MIME-Version: 1.0
X-MailScanner: Found to be clean
X-MailScanner-SpamScore: s
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

According to ams AT ludd DOT luth DOT se:
> Remember I talked about a stack size option to stubify? I've added
> code to get an environment variable's value to choose the stack
> size. This way those that do want a big stack size can set it and it
> won't affect us that doesn't want it, except when we run programs
> somebody else built. (Or possibly the other way round.)

This turned out to be a sucker.

1. First I tried to copy code from stubedit and was thuroughly
confused by offsets. (There are at least three different ones to be
aware of.)

2. Then it still wouldn't work. With extremely weird
results. E. g. this code:

  unsigned char filehdr_buf[20];
  unsigned long exe_start;
...
    if (lseek(ofile, 8L, SEEK_SET) != 8L)
    {
      error();
    }	
    if (read(ofile, filehdr_buf, 2) != 2)
    {
      error();
    }	
    exe_start = filehdr_buf[0]*16 + filehdr_buf[1]*16*256;
    fprintf(stderr, "exe_start = %lu.\n", exe_start);

claimed exe_start to be above 86000 (or perhaps it was 860000).

Until run in gdb, there it was 512 (as expected)!!

3. Meanwhile I discovered gdb's command "x/12wx $esp" doesn't seem to
work. I see some values but it sure doesn't look what I expect is on
the stack. (E. g. after calling lseek(3, 526, 0), I'd expect to see 0,
526, 3 and <return EIP> on the stack.) I'm using gdb 5.1.1.


So I junked that and coded up calling stubedit instead, which in
retrospect I think I should have gone for from the beginning.

So here's the patch.

Note that it includes a bugfix, where the old code wouldn't print the
name of the target file on errors (the "ofname"-> "ofilename"
changes) as well.


Right,

						MartinS

Index: djgpp/src/stub/stubify.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/stub/stubify.c,v
retrieving revision 1.4
diff -p -u -r1.4 stubify.c
--- djgpp/src/stub/stubify.c	14 Dec 1999 12:01:36 -0000	1.4
+++ djgpp/src/stub/stubify.c	1 Jan 2003 18:30:10 -0000
@@ -1,3 +1,4 @@
+/* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */
 /* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */
 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
@@ -7,13 +8,18 @@
 #include <sys/stat.h>
 #include <string.h>
 #include <unistd.h>
+#include <ctype.h>
 #ifdef __DJGPP__
 #include <io.h>
 #include <libc/dosio.h>
 #include <go32.h>
 #include <dpmi.h>
 #include <errno.h>
+#endif
+
+#include "../../include/stubinfo.h"
 
+#ifdef __DJGPP__
 #define tbsize _go32_info_block.size_of_transfer_buffer
 #endif
 
@@ -21,6 +27,9 @@
 #define O_BINARY 0
 #endif
 
+/* Environment variable to check whether the user wants a custom minimum stack size. */
+#define MINSTACK_ENV_VAR "STUBIFY_MINSTACK"
+
 const unsigned char stub_bytes[] = {
 #include "stub.h"
 };
@@ -46,7 +55,7 @@ void coff2exe(char *fname)
   char ofilename[256];
   int ifile;
   int ofile;
-  char *ofname, *ofext;
+  char *ofname, *ofext, *env_stack_size;
   char buf[4096];
   int rbytes, used_temp = 0, i, n;
   long coffset=0;
@@ -138,7 +147,7 @@ void coff2exe(char *fname)
   ofile = open(ofilename, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0777);
   if (ofile < 0)
   {
-    perror(ofname);
+    perror(ofilename);
     return;
   }
   v_printf("stubify: %s -> %s", ifilename, ofilename);
@@ -199,7 +208,7 @@ void coff2exe(char *fname)
 #endif
     if (wb < 0)
     {
-      perror(ofname);
+      perror(ofilename);
       close(ifile);
       close(ofile);
       unlink(ofilename);
@@ -207,7 +216,7 @@ void coff2exe(char *fname)
     }
     if (wb < rbytes)
     {
-      fprintf(stderr, "%s: disk full\n", ofname);
+      fprintf(stderr, "%s: disk full\n", ofilename);
       close(ifile);
       close(ofile);
       unlink(ofilename);
@@ -231,6 +240,43 @@ void coff2exe(char *fname)
       perror("The error was");
     }
   }
+
+  /* Now adjust stack size if necessary, i. e. use value from environment if set. */
+  env_stack_size = getenv(MINSTACK_ENV_VAR);
+  if (env_stack_size)
+  {
+    unsigned long stack_size = 0;
+    char multiplier = 0;
+    char *stubedit_str;
+
+    if( 0 < sscanf(env_stack_size, "%li%c", &stack_size, &multiplier) &&
+	( (multiplier == 0 || toupper(multiplier) == 'K' || toupper(multiplier) == 'M') ) )
+    {
+      v_printf("Using " MINSTACK_ENV_VAR " = %s.\n", env_stack_size);
+
+      stubedit_str = malloc( sizeof("stubify ")+strlen(ofilename)+sizeof(" minstack=")+strlen(env_stack_size)+1 );
+      if( stubedit_str )
+      {
+	sprintf(stubedit_str, "stubedit %s minstack=%s", ofilename, env_stack_size);
+	i = system(stubedit_str);
+	if( i )
+	{
+	  fprintf(stderr, "%s: failed setting requested minimum stack size (stubedit exit status was %d)\n", ofilename, i);
+	}
+	
+	free( stubedit_str );
+      }
+      else
+      {
+	fprintf(stderr, "%s: failed setting requested minimum stack size (out of memory)\n", ofilename);
+      }
+    }
+    else
+    {
+      fprintf(stderr, "%s: illegal minimum stack size specified, check environment variable " MINSTACK_ENV_VAR "\n", ofilename);
+    }
+  }
+
 }
 
 int main(int argc, char **argv)


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019