From owner-freebsd-hackers Sat Nov 21 23:03:18 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id XAA09987 for freebsd-hackers-outgoing; Sat, 21 Nov 1998 23:03:18 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from mail2.rochester.rr.com (mail2-0.twcny.rr.com [24.92.226.75]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id XAA09978 for ; Sat, 21 Nov 1998 23:03:16 -0800 (PST) (envelope-from leisner@rochester.rr.com) Received: from rochester.rr.com ([24.93.25.38]) by mail2.rochester.rr.com (Post.Office MTA v3.5.2 release 221 ID# 0-53939U80000L80000S0V35) with ESMTP id com; Sun, 22 Nov 1998 02:03:20 -0500 Received: from dw (localhost [127.0.0.1]) by rochester.rr.com (8.8.5/8.8.5) with ESMTP id CAA01604; Sun, 22 Nov 1998 02:02:43 -0500 Message-Id: <199811220702.CAA01604@rochester.rr.com> Reply-to: leisner@rochester.rr.com To: Nate Williams cc: hackers@FreeBSD.ORG Subject: Re: Wrapping a function In-reply-to: Your message of "Mon, 16 Nov 1998 22:17:47 MST." <199811170517.WAA22627@mt.sri.com> Date: Sun, 22 Nov 1998 02:02:43 -0500 From: "Marty Leisner" Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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