delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/01/05/09:21:48

Sender: rich AT phekda DOT freeserve DOT co DOT uk
Message-ID: <3E183620.EDBE0071@phekda.freeserve.co.uk>
Date: Sun, 05 Jan 2003 13:41:52 +0000
From: Richard Dawe <rich AT phekda DOT freeserve DOT co DOT uk>
X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.2.23 i586)
X-Accept-Language: de,fr
MIME-Version: 1.0
To: djgpp-workers AT delorie DOT com
Subject: Re: stubify
References: <200301011849 DOT h01InxE21061 AT speedy DOT ludd DOT luth DOT se>
Reply-To: djgpp-workers AT delorie DOT com

Hello.

ams AT ludd DOT luth DOT se wrote:
[snip]
> 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.

Does it work with 5.2.1 or 5.3?

The call to lseek goes through a stub to get to __lseek, but that's a jmp not
a call, so I guess it makes no difference to the stack.

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

That seems like the right way, to avoid code duplication. (Although we could
have another source file with the "stack size" setting code in it.)
 
> So here's the patch.

See my comments inline below.
 
> 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') ) )

What happens to the multiplier? It doesn't seem to be used.

Shouldn't the format be %lu%c, since stack_size is an unsigned long?

> +    {
> +      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 );

Why do you do sizeof("stubify ")? Shouldn't it be strlen? And on "stubedit "?
I.e.: strlen("stubedit ")? As it turns out, the number from the above code is
right.

> +      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);

You could display the contents of the environment variable here.

> +    }
> +  }
> +
>  }
> 
>  int main(int argc, char **argv)

Apart from those comments, it looks good to me. Although I hope the code is
readable without line wrapping in 80 columns.

Thanks, bye, Rich =]

-- 
Richard Dawe [ http://www.phekda.freeserve.co.uk/richdawe/ ]

- Raw text -


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