Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 22 Nov 1998 02:02:43 -0500
From:      "Marty Leisner" <leisner@rochester.rr.com>
To:        Nate Williams <nate@mt.sri.com>
Cc:        hackers@FreeBSD.ORG
Subject:   Re: Wrapping a function 
Message-ID:  <199811220702.CAA01604@rochester.rr.com>
In-Reply-To: Your message of "Mon, 16 Nov 1998 22:17:47 MST." <199811170517.WAA22627@mt.sri.com> 

next in thread | previous in thread | raw e-mail | index | archive | help


In recent gnu ld's:
`--wrap SYMBOL'
     Use a wrapper function for SYMBOL.  Any undefined reference to
     SYMBOL will be resolved to `__wrap_SYMBOL'.  Any undefined
     reference to `__real_SYMBOL' will be resolved to SYMBOL.

     This can be used to provide a wrapper for a system function.  The
     wrapper function should be called `__wrap_SYMBOL'.  If it wishes
     to call the system function, it should call `__real_SYMBOL'.

     Here is a trivial example:

          void *
          __wrap_malloc (int c)
          {
            printf ("malloc called with %ld\n", c);
            return __real_malloc (c);
          }

     If you link other code with this file using `--wrap malloc', then
     all calls to `malloc' will call the function `__wrap_malloc'
     instead.  The call to `__real_malloc' in `__wrap_malloc' will call
     the real `malloc' function.

     You may wish to provide a `__real_malloc' function as well, so that
     links without the `--wrap' option will succeed.  If you do this,
     you should not put the definition of `__real_malloc' in the same
     file as `__wrap_malloc'; if you do, the assembler may resolve the
     call before the linker has a chance to wrap it to `malloc'.



In message <199811170517.WAA22627@mt.sri.com>,   you write:
>Does anyone have an easy way of 'wrapping' an already existing library
>function so that any programs linked against your .o will call your
>function, but so your function can call the 'real' library function?
>
>Example:
>
>my_malloc.c:
>
>void *malloc(size_t size)
>{
>    void *ret;
>
>    printf("Calling malloc\n");
>    ret = REALMALLOC(size);
>    printf("Leaving malloc\n");
>    return ret;
>}
>
>Ignoring all of the functions where there is loss of errno and such, are
>they any good ideas?  Note, the use of the dl* functions is explicitly
>not allowed since those happen to be the functions I want to wrap in
>this case.
>
>I'm at a loss here how to do this in C, so any good hacks are welcomed.
>
>
>Nate
>
>To Unsubscribe: send mail to majordomo@FreeBSD.org
>with "unsubscribe freebsd-hackers" in the body of the message

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199811220702.CAA01604>