* "ld --wrapper" but without changing legacy executable, possible?
@ 2006-03-21 1:23 sean yang
2006-03-21 1:26 ` Paul Brook
0 siblings, 1 reply; 4+ messages in thread
From: sean yang @ 2006-03-21 1:23 UTC (permalink / raw)
To: binutils
Actually, the title of this post should be "Is run-time wrapper with
lightweight overhead possible?" I appreciate your patience to read and
answer.
Suppose I want to achieve the following effect: any libc call exept malloc
should behave as is, and malloc should behave as my wrapper function defines
(say, printf a string before malloc).
-----------------------------------------------------------------------------
//mallocwrapper.c
void *
__wrap_ malloc(int c)
{
printf ("any malloc should be intercepted\n", c);
return __real_malloc (c);
}
-------------------------------------------------------------------------------
Apparently, if I have access to application source(see test.c), I can use a
ld -wrap (thanks to Nick and Daniel for answering my previous question) to
achive such a goal. i.e., somethink like
>gcc -c mallocwrapper.c
>ar libmallocwrapper.a mallocwrapper.o
>gcc -o test test.c -lmallocwrapper --wrap malloc
-----------------------------------------------------------------------------
//test.c
#include <stdio.h>
#include <stdlib.h>
int main(){
int i = 4;
void *ptr;
printf("this should invoke the original printf in libc.so\n");
ptr= malloc(i);
free (ptr);
ptr= malloc(i);
free (ptr);
}
-------------------------------------------------------------------------------
But above approach works only if I have access to the application's source
code (test.c). If my legacy executable code (`test') has already been
compiled and linked to libc.so.6, and I don't have test.c, then above
approach doesn't work.
Though I can use a LD_PRELOAD variable to sepcify wrapper at runtime in this
case, the overhead is very high, i.e., the actually wrapper need
dlsym(RTLD_NEXT, "malloc") to open the actual malloc each time it's called.
Please see the following code and command line.
-----------------------------------------------------------------------------
//runtimewrapper.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <dlfcn.h>
4 void* malloc(int size) {
5 printf("wrapped malloc\n");
6 void* (*real_malloc)(const char*, const char*) =
7 dlsym(RTLD_NEXT, "malloc");
8 return real_malloc(size);
9 }
-------------------------------------------------------------------------------
>gcc -fPIC -rdynamic -c runtimewrapper.c
>gcc -shared -o libruntimewrapper.so runtimewrapper.o -lc -ldl
>LD_LIBRARY_PATH=./ LD_PRELOAD=libruntimewrapper.so ./test
But the problem of above approach is that " void* (*real_malloc)(const
char*, const char*) = dlsym(RTLD_NEXT, "malloc");" is called each time the
application want to use malloc(), which is the major source of overhead. As
you can see from my example, what I really want to do is just a
"printf("wrapped malloc\n");" before each real malloc.
My question is: is there anyway to avoid this overhead? Hopefully, my
question is clear and get some advice. Thanks a lot.
Sean
_________________________________________________________________
Is your PC infected? Get a FREE online computer virus scan from McAfee®
Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: "ld --wrapper" but without changing legacy executable, possible?
2006-03-21 1:23 "ld --wrapper" but without changing legacy executable, possible? sean yang
@ 2006-03-21 1:26 ` Paul Brook
2006-03-21 2:23 ` sean yang
0 siblings, 1 reply; 4+ messages in thread
From: Paul Brook @ 2006-03-21 1:26 UTC (permalink / raw)
To: binutils; +Cc: sean yang
> //runtimewrapper.c
> 1 #define _GNU_SOURCE
> 2 #include <stdio.h>
> 3 #include <dlfcn.h>
> 4 void* malloc(int size) {
> 5 printf("wrapped malloc\n");
> 6 void* (*real_malloc)(const char*, const char*) =
> 7 dlsym(RTLD_NEXT, "malloc");
> 8 return real_malloc(size);
> 9 }
>...
> But the problem of above approach is that " void* (*real_malloc)(const
> char*, const char*) = dlsym(RTLD_NEXT, "malloc");" is called each time the
> application want to use malloc(), which is the major source of overhead.
Unless I'm missing something the answer is "Don't do that then". ie. only call
dlsym the first time the wrapper is run.
Paul
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: "ld --wrapper" but without changing legacy executable, possible?
2006-03-21 1:26 ` Paul Brook
@ 2006-03-21 2:23 ` sean yang
2006-03-21 14:27 ` Andreas Schwab
0 siblings, 1 reply; 4+ messages in thread
From: sean yang @ 2006-03-21 2:23 UTC (permalink / raw)
To: paul, binutils
I know we can do a if(!=NULL) to get it around.
I guess there could be some clever way, say analogy to the .PLT entry
filling.
>From: Paul Brook <paul@codesourcery.com>
>To: binutils@sourceware.org
>CC: "sean yang" <seanatpurdue@hotmail.com>
>Subject: Re: "ld --wrapper" but without changing legacy executable,
>possible?
>Date: Tue, 21 Mar 2006 01:23:23 +0000
>
> > //runtimewrapper.c
> > 1 #define _GNU_SOURCE
> > 2 #include <stdio.h>
> > 3 #include <dlfcn.h>
> > 4 void* malloc(int size) {
> > 5 printf("wrapped malloc\n");
> > 6 void* (*real_malloc)(const char*, const char*) =
> > 7 dlsym(RTLD_NEXT, "malloc");
> > 8 return real_malloc(size);
> > 9 }
> >...
> > But the problem of above approach is that " void* (*real_malloc)(const
> > char*, const char*) = dlsym(RTLD_NEXT, "malloc");" is called each time
>the
> > application want to use malloc(), which is the major source of overhead.
>
>Unless I'm missing something the answer is "Don't do that then". ie. only
>call
>dlsym the first time the wrapper is run.
>
>Paul
_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: "ld --wrapper" but without changing legacy executable, possible?
2006-03-21 2:23 ` sean yang
@ 2006-03-21 14:27 ` Andreas Schwab
0 siblings, 0 replies; 4+ messages in thread
From: Andreas Schwab @ 2006-03-21 14:27 UTC (permalink / raw)
To: sean yang; +Cc: paul, binutils
"sean yang" <seanatpurdue@hotmail.com> writes:
> I know we can do a if(!=NULL) to get it around.
> I guess there could be some clever way, say analogy to the .PLT entry
> filling.
Call dlsym in a constructor function.
Andreas.
--
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, MaxfeldstraÃe 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-03-21 12:01 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-21 1:23 "ld --wrapper" but without changing legacy executable, possible? sean yang
2006-03-21 1:26 ` Paul Brook
2006-03-21 2:23 ` sean yang
2006-03-21 14:27 ` Andreas Schwab
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).