From owner-freebsd-arch@FreeBSD.ORG Fri Jan 18 13:05:32 2013 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id A5CC6974 for ; Fri, 18 Jan 2013 13:05:32 +0000 (UTC) (envelope-from stevek@juniper.net) Received: from exprod7og127.obsmtp.com (exprod7og127.obsmtp.com [64.18.2.210]) by mx1.freebsd.org (Postfix) with ESMTP id 4B24A76B for ; Fri, 18 Jan 2013 13:05:32 +0000 (UTC) Received: from P-EMHUB03-HQ.jnpr.net ([66.129.224.36]) (using TLSv1) by exprod7ob127.postini.com ([64.18.6.12]) with SMTP ID DSNKUPlIm/k9VHkbsc9AHk6IkvXCBrNhfmGW@postini.com; Fri, 18 Jan 2013 05:05:32 PST Received: from stevek-ubuntu (172.25.4.212) by P-EMHUB03-HQ.jnpr.net (172.24.192.33) with Microsoft SMTP Server id 8.3.213.0; Fri, 18 Jan 2013 04:53:31 -0800 Date: Fri, 18 Jan 2013 07:53:21 -0500 From: Steve Kiernan To: Konstantin Belousov 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> <20130117184654.06f8e330@stevek-ubuntu> <20130118040933.GU2522@kib.kiev.ua> <20130117232850.1b69bfc0@stevek-ubuntu> <20130118052939.GV2522@kib.kiev.ua> Organization: Juniper Networks Inc. X-Mailer: Claws Mail 3.8.0 (GTK+ 2.24.10; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/signed; micalg=PGP-SHA1; boundary="Sig_/f/FxglD/52jn=PCB4c53jsQ"; protocol="application/pgp-signature" Cc: Jason Evans , freebsd-arch@freebsd.org X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 18 Jan 2013 13:05:32 -0000 --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 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 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--