Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 18 Jan 2013 07:53:21 -0500
From:      Steve Kiernan <stevek@juniper.net>
To:        Konstantin Belousov <kostikbel@gmail.com>
Cc:        Jason Evans <jasone@canonware.com>, freebsd-arch@freebsd.org
Subject:   Re: [JNPR] Proposal to add weak symbols for malloc, realloc, and free to libc
Message-ID:  <20130118075321.5c1ae9ee@stevek-ubuntu>
In-Reply-To: <20130118052939.GV2522@kib.kiev.ua>
References:  <20130117161311.4c15c7c4@stevek-ubuntu> <E4A1FEFD-B267-422B-8051-443B1F8519E1@canonware.com> <20130117184654.06f8e330@stevek-ubuntu> <20130118040933.GU2522@kib.kiev.ua> <20130117232850.1b69bfc0@stevek-ubuntu> <20130118052939.GV2522@kib.kiev.ua>

next in thread | previous in thread | raw e-mail | index | archive | help
--Sig_/f/FxglD/52jn=PCB4c53jsQ
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: quoted-printable

On Fri, 18 Jan 2013 07:29:39 +0200
Konstantin Belousov <kostikbel@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@gmail.com> wrote:
> >=20
> > > 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.
> >=20
> > 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.
>=20
> Well, the standard technique is to do
>  void *libc_handle =3D dlopen("libc.so");
>  void *(*libc_malloc)(size_t) =3D dlsym(libc_handle, "malloc");
> if you know exactly that you want malloc from libc.
>=20
> 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

--Sig_/f/FxglD/52jn=PCB4c53jsQ
Content-Type: application/pgp-signature; name="signature.asc"
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAlD5RcIACgkQZSuJlLuTi6htmgCePNcIUbEkle0pEgZ3JdZcXWvq
td8AoMQt2dJipVxvrB0So2pkR0UwPK/3
=iBHT
-----END PGP SIGNATURE-----

--Sig_/f/FxglD/52jn=PCB4c53jsQ--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20130118075321.5c1ae9ee>