[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: hook operator new

From: Paul Pluzhnikov
Subject: Re: hook operator new
Date: Sun, 14 Oct 2007 12:18:09 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

skaller <> writes:

> So I have another (yes I tried to find the answer but haven't yet):
> how can I hook operator new()?

This is very system-dependent.
Are you interested in Linux-only answer, or in gcc/any answer?

> With malloc() we have __builtin_malloc which is a single global variable
> available to all shared libraries.

Only on systems using glibc, and only when using dynamic linking.

> operator new() is a weak symbol in Elf isn't it?

Not for any of my g++ versions:

$ nm -A /usr/local/gcc-*/lib/libstdc++*so | grep _Znwj 
/usr/local/gcc-3.1/lib/ T _Znwj
/usr/local/gcc-3.1/lib/ T _ZnwjRKSt9nothrow_t
/usr/local/gcc-3.2/lib/ T _Znwj
/usr/local/gcc-3.2/lib/ T _ZnwjRKSt9nothrow_t
/usr/local/gcc-3.3.2/lib/ T _Znwj
/usr/local/gcc-3.3.2/lib/ T _ZnwjRKSt9nothrow_t
/usr/local/gcc-3.3/lib/ T _Znwj
/usr/local/gcc-3.3/lib/ T _ZnwjRKSt9nothrow_t
/usr/local/gcc-3.4.0/lib/ T _Znwj
/usr/local/gcc-3.4.0/lib/ T _ZnwjRKSt9nothrow_t
/usr/local/gcc-3.4.3/lib/ T _Znwj
/usr/local/gcc-3.4.3/lib/ T _ZnwjRKSt9nothrow_t
/usr/local/gcc-3.4.6/lib/ T _Znwj
/usr/local/gcc-3.4.6/lib/ T _ZnwjRKSt9nothrow_t
/usr/local/gcc-4.0.0/lib/ T _Znwj
/usr/local/gcc-4.0.0/lib/ T _ZnwjRKSt9nothrow_t
/usr/local/gcc-4.1.1/lib/ T _Znwj
/usr/local/gcc-4.1.1/lib/ T _ZnwjRKSt9nothrow_t
/usr/local/gcc-4.2.0/lib/ T _Znwj
/usr/local/gcc-4.2.0/lib/ T _ZnwjRKSt9nothrow_t
/usr/local/gcc-4.3-20061104/lib/ T _Znwj
/usr/local/gcc-4.3-20061104/lib/ T _ZnwjRKSt9nothrow_t

None of the above is weak...

> So it can be replaced by a user function globally? (even in by a shared 
> library?)

Weak here doesn't mean what you appear to think it means.

Even though above symbols are all strong, they can still be
interposed by a user library, *provided* that library appears before
libstdc++ in runtime linker search list. To achive that, make sure
your interposer is before -lstdc++ on the link line.

> [Of course C++ Standard is quite stupid here. If you replace operator
> new, how do you call the original?]

The "standard" Linux/Solaris technique is to use dlsym(RTLD_NEXT, ...)

In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]