[JNPR] Proposal to add weak symbols for malloc, realloc, and free to libc

Steve Kiernan stevek at juniper.net
Fri Jan 18 13:05:32 UTC 2013


On Fri, 18 Jan 2013 07:29:39 +0200
Konstantin Belousov <kostikbel at gmail.com> wrote:

> On Thu, Jan 17, 2013 at 11:28:50PM -0500, Steve Kiernan wrote:
> > On Fri, 18 Jan 2013 06:09:33 +0200
> > Konstantin Belousov <kostikbel at gmail.com> wrote:
> > 
> > > That said, what are the difficulties you experiencing with the malloc
> > > interposing ? According to the normal ELF symbol lookup rules, the
> > > definitions from any object which is loaded before libc overrides the
> > > libc symbols.
> > 
> > The problem is when one want to augment the calls. For example, say
> > you want to do some leak detection or keep track of statistics that
> > the malloc library does not. One would need to still call the original
> > call after replacing the malloc/realloc/free/etc. with their own.
> > Without having the __malloc/__realloc/__free/etc. that _used_ to be
> > in libc, one cannot do so.
> 
> Well, the standard technique is to do
>  void *libc_handle = dlopen("libc.so");
>  void *(*libc_malloc)(size_t) = dlsym(libc_handle, "malloc");
> if you know exactly that you want malloc from libc.
> 
> If you want just any previous interposer for the malloc, then the right
> technique is to do dlsym(RTLD_NEXT, "malloc") from your malloc wrapper.

That is only guaranteed to work if you're linking with libc dynamically.
If you statically link, that may not necessarily work. Having the weak
symbols ensures things will work correctly no matter which linking method
one uses.

-Steve
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-arch/attachments/20130118/268a9cb4/attachment.sig>


More information about the freebsd-arch mailing list