This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: problems with ld --wrap : Help!


On Tue, 5 Sep 2000, William Gacquer wrote:

> 	I am attempting to wrap malloc using "ld --wrap malloc" as in the ld
> documentation. 
> 	but I always get a "__real_malloc undeclared" error message.

Here is an example that may help you.

cat > wrap.c <<EOF
void *__real_malloc (int);

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

cat > callwrap.c <<EOF
void *malloc (int);

int main (void)
{
  return malloc (1000) == 0;
}
EOF

gcc -c -o wrap.o wrap.c
gcc -c -o callwrap.o callwrap.c

nm wrap.o
         U __real_malloc
00000000 T __wrap_malloc
00000000 t gcc2_compiled.
         U printf
nm callwrap.o
00000000 t gcc2_compiled.
00000000 T main
         U malloc

ld -wrap malloc callwrap.o wrap.o
ld: warning: cannot find entry symbol _start; defaulting to 08048080
wrap.o: In function `__wrap_malloc':
wrap.o(.text+0x14): undefined reference to `printf'
wrap.o(.text+0x1d): undefined reference to `malloc'

Notice how wrap.o has __real_malloc (and printf) undefined, while
callwrap.o has malloc undefined.  "ld -wrap malloc" transforms the
reference to `malloc' in callwrap.o into a reference to `__wrap_malloc',
which is satisfied by wrap.o  ld also transforms the reference to
`__real_malloc' in wrap.o into plain `malloc', which is reported as
undefined along with printf because I didn't link against the library.

Now if we let gcc do the link, and specify libraries for us:

gcc -Wl,-wrap,malloc callwrap.o wrap.o
./a.out
malloc called with 1000

Everything works as it should, at least under i586-linux

Alan Modra
-- 
Linuxcare.  Support for the Revolution.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]