From owner-freebsd-hackers@freebsd.org Tue Jul 2 09:56:49 2019 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id AC02415CE1E7 for ; Tue, 2 Jul 2019 09:56:49 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id D89FC76AA0; Tue, 2 Jul 2019 09:56:48 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id x629ue54071649 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Tue, 2 Jul 2019 12:56:43 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua x629ue54071649 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id x629ud6B071648; Tue, 2 Jul 2019 12:56:39 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Tue, 2 Jul 2019 12:56:39 +0300 From: Konstantin Belousov To: Baptiste Daroussin Cc: Yuri , Freebsd hackers list , Shawn Webb Subject: Re: Question: Is there a way to get the list of all dynamic libraries needed by a given elf through API? Message-ID: <20190702095639.GA47193@kib.kiev.ua> References: <20190702031523.lhlrqnlo7pxccl42@mutt-hbsd> <20190702071200.qy6hr5yhaczhj6wx@ivaldir.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190702071200.qy6hr5yhaczhj6wx@ivaldir.net> User-Agent: Mutt/1.12.1 (2019-06-15) X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on tom.home X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 02 Jul 2019 09:56:49 -0000 On Tue, Jul 02, 2019 at 09:12:00AM +0200, Baptiste Daroussin wrote: > On Mon, Jul 01, 2019 at 10:27:25PM -0700, Yuri wrote: > > On 2019-07-01 20:15, Shawn Webb wrote: > > > $ ldd -f '%p\n' /bin/ls > > > > > > Hi Shawn, > > > > > > ldd doesn't do what I need. It sets one specific environment variable, then > > forks and calls execl(3). > > > > And I am looking for the API function like: char* > > find_shared_library_mapping(const char *so_lib, char *buf_out, size_t > > buf_out_sz); // or something like this. > > > Use libelf, and read the dynamic section which will give you everything you > want: > > readelf -d which uses libelf, will give you this with all the NEEDED tag. > The same way you can get the runpath and rpath > > Tag Type Name/Value > 0x0000000000000001 NEEDED Shared library: [libutil.so.9] > 0x0000000000000001 NEEDED Shared library: [libncursesw.so.8] > 0x0000000000000001 NEEDED Shared library: [libc.so.7] > > The good thing about it is that you will you will only get the direct > dependency. > > This is through this lib that pkg extract the list of required libraries Rtld rules for search paths preferences are very convoluted. Look at the ld-elf.so.1 man page about it, rpath vs. runpath, and the effects of the environment variables, and the caller settings, and tags expansion which rewrites DT_NEEDED and rpath. The question 'which file is used to satisfy this DT_NEEDED tag' can be only correctly answered in context of the image activation or dlopen(3) call, due to all the factors affecting the selection. Even the location of the code which called dlopen(3) is important. The consequence is that such API cannot exist in reliable form.