help-gplusplus
[Top][All Lists]
Advanced

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

Re: hook operator new


From: skaller
Subject: Re: hook operator new
Date: Mon, 15 Oct 2007 15:51:54 -0000
User-agent: Pan/0.129 (Benson & Hedges Moscow Gold)

On Sun, 14 Oct 2007 12:18:09 -0700, Paul Pluzhnikov wrote:

> skaller <skaller@users.sf.net> 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?

All platforms all compilers, incl. MSVC++ .. but just
Linux/gcc will do for a start :)

>> 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.

Really? Doesn't work for static links .. hmm.
 
>> operator new() is a weak symbol in Elf isn't it?
> 
> Not for any of my g++ versions:

Then, how can one replace it? Because that definitely does work
in a simple program.

>> 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.

Well, -lstdc++ is never given on the link line, in fact, there's
no link line since most people use gcc as the link driver.

>> [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, ...)

Which would only work with dynamic linkage. Hmm .. I seem to recall
Boehm gc (which has to do all this somehow) uses a special file
defining real_malloc() by calling malloc() .. and linking that
before the code that defines malloc() by suballocating and sometimes
calling real_malloc() .. ouch.



-- 
John Skaller <skaller at users dot sf dot net>
Try Felix, the successor to C++ http://felix.sf.net



reply via email to

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