Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 2 Jul 2019 12:56:39 +0300
From:      Konstantin Belousov <kostikbel@gmail.com>
To:        Baptiste Daroussin <bapt@FreeBSD.org>
Cc:        Yuri <yuri@rawbw.com>, Freebsd hackers list <freebsd-hackers@freebsd.org>,  Shawn Webb <shawn.webb@hardenedbsd.org>
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>
In-Reply-To: <20190702071200.qy6hr5yhaczhj6wx@ivaldir.net>
References:  <dfac779f-fa4f-a292-1a76-a9e7db5e024a@rawbw.com> <20190702031523.lhlrqnlo7pxccl42@mutt-hbsd> <c04b30b9-8f1f-15dc-f93d-e3d8cd1f5fa0@rawbw.com> <20190702071200.qy6hr5yhaczhj6wx@ivaldir.net>

next in thread | previous in thread | raw e-mail | index | archive | help
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.



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