From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/8112 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Making a shared library that intercepts call to main work with musl compiled binary Date: Mon, 6 Jul 2015 15:06:51 -0400 Message-ID: <20150706190651.GY1173@brightrain.aerifal.cx> References: <20150706173259.GX1173@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1436209629 9479 80.91.229.3 (6 Jul 2015 19:07:09 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 6 Jul 2015 19:07:09 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-8125-gllmg-musl=m.gmane.org@lists.openwall.com Mon Jul 06 21:07:07 2015 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1ZCBjP-00055I-Fi for gllmg-musl@m.gmane.org; Mon, 06 Jul 2015 21:07:07 +0200 Original-Received: (qmail 30124 invoked by uid 550); 6 Jul 2015 19:07:06 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 30106 invoked from network); 6 Jul 2015 19:07:05 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:8112 Archived-At: On Mon, Jul 06, 2015 at 10:00:41PM +0300, Alexander Monakov wrote: > On Mon, 6 Jul 2015, Rich Felker wrote: > > On Mon, Jul 06, 2015 at 01:00:52PM -0400, Riyad Parvez wrote: > > > Hi All, > > > > > > I've shared library [1] which intercepts loading of a binary and does some > > > pre-processing with the command-line arguments via "LD_PRELOAD" technique. > > > The shared library works fine with glibc compiled binary i.e., it can > > > intercept the call to main and does some preprocessing. But it can't > > > intercept call to "main" in musl-compiled binary. I've compiled my binary > > > in static mode with musl "CC=/musl-gcc -static". How can I make the shared > > > library work with musl-compiled binary? > > > > > > Thanks > > > Riyad > > > > > > [1]: https://github.com/dslab-epfl/s2e/blob/master/guest/init_env/init_env.c > > > > I haven't read this code in detail, so I don't have any advice on > > whether there are other problems, but the basic issue seems to be that > > you're static linking. The same would happen if you use glibc and > > static link. If you want the ability to use LD_PRELOAD you need to > > link dynamically. > > Furthermore, even with dynamic linking you cannot interpose 'main' in the > executable via LD_PRELOAD. Even more baffling is that the linked code > interposes __libc_start_main; I'm lost for words that it happens to work with > glibc. Presumably it interposes __libc_start_main because it can't interpose main. As far as I can tell the only reason it does this is to inject fake argv. I'm skeptical as to whether it works correctly. > I think a cleaner solution can be obtained by converting __s2e_init_env in the > linked code to a constructor that examines environment variables, and moving > the command line processing to an separate wrapper executable. Certainly. Rich