Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 17 Nov 1998 08:48:40 -0500 (EST)
From:      Thomas David Rivers <rivers@dignus.com>
To:        hackers@FreeBSD.ORG, nate@mt.sri.com
Subject:   Re: Wrapping a function
Message-ID:  <199811171348.IAA11670@lakes.dignus.com>
In-Reply-To: <199811170517.WAA22627@mt.sri.com>

next in thread | previous in thread | raw e-mail | index | archive | help
> 
> 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
> 

 I don't know about our linker (gnu-ld) - but many UNIX linkers (and other
systems as well) have a way to re-name identifiers.

 The standard trick would be to pre-link the malloc code from the library;
renaming the malloc entry there - to say, MALLOC.  (ld -r is how you 
prelink.)

 Then, you can link your code with that.

	- Dave Rivers -

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?199811171348.IAA11670>