Question: Is there a way to get the list of all dynamic libraries needed by a given elf through API?

Shawn Webb shawn.webb at hardenedbsd.org
Tue Jul 2 03:15:27 UTC 2019


On Mon, Jul 01, 2019 at 07:54:27PM -0700, Yuri wrote:
> I want to get the same information that 'ldd <elf-file>' returns, but from a
> C/C++ program without launching external processes.
> 
> It is possible to find the list of elf's shared library dependencies using
> libelf. But I couldn't find how to map library names to full library paths.
> 
> 
> ldd(1) sets a certain environment variable, forks and calls execl(3), and
> the "ld.so magic" does the rest.
> 
> 
> Obviously, if one needs to do the same from their own C program, forking is
> a much less attractive option compared to the API way.
> 
> 
> Is there an API to find such mappings? If no, why doesn't it exist?

Hey Yuri,

If you're not afraid to get your hands, feet, and pet rock named Steve
dirty with the one true language of love, C, and you can link against
libraries in base, take a look at libelf.

If you want to use a scripting language, ldd is still the right tool
for the job. Taking a look at the manpage for ldd(1), which will
eventually point us to rtld(1), which will teach us that we can end up
with something like:

$ ldd -f '%p\n' /bin/ls
/lib/libutil.so.9
/lib/libncursesw.so.8
/lib/libthr.so.3
/lib/librt.so.1
/lib/libm.so.5
/lib/libexecinfo.so.1
/lib/libc.so.7

(That is verbatim on my HardenedBSD dev laptop. The multitude of
libraries is due to SafeStack and CFI.)

So, what you're looking for is technically documented, but not in a
straightforward manner.

Thanks,

-- 
Shawn Webb
Cofounder / Security Engineer
HardenedBSD

Tor-ified Signal:    +1 443-546-8752
Tor+XMPP+OTR:        lattera at is.a.hacker.sx
GPG Key ID:          0xFF2E67A277F8E1FA
GPG Key Fingerprint: D206 BB45 15E0 9C49 0CF9  3633 C85B 0AF8 AB23 0FB2
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20190701/270c9635/attachment.sig>


More information about the freebsd-hackers mailing list