[TUTORIAL] Find out which shared libs (.so) are missing

Search This thread

tuxboy

Member
Dec 19, 2013
48
232
Dhaka
www.adhikary.net
While porting features or ROMs from one Android device to another, some of us frequently encounter missing shared libs error. The process of finding missing shared libraries (.so files) might be a bit trivial, and time consuming. This tutorial is going to demonstrate a one-command way of finding all the dependencies of an executable, or another shared library.

FIRST TIME CONFIGURATION
Open up your terminal, type in:
Code:
$ echo 'readelf -d $1 | grep "\(NEEDED\)" | sed -r "s/.*\[(.*)\]/\1/"' | sudo tee -a /usr/local/bin/ldd-arm
$ sudo chmod +x /usr/local/bin/ldd-arm

[Member @_that told about this (better) way of performing this, without NDK and a more readable output. Thanks!]

FINDING MISSING LIBS

Basic usage is very easy. In your terminal:

$ ldd-arm PATH_TO_YOUR_EXECUTABLE_OR_LIBRARY

Say, for example, we want to find out the dependecies (which shared libraries are required) of my sensor HAL. I would type in terminal,

Code:
$ ldd-arm sensors.msm7x27a.so

And, I get the desired output!

Code:
liblog.so
libutils.so
libcutils.so
libhardware.so
libdl.so
libmemalloc.so
libc.so
libstdc++.so
libm.so

And that's how you can find out the libraries you are missing!
 
Last edited:

dummie999

Senior Member
Mar 2, 2014
721
413
Awesome tutorial! But are you sure
Code:
echo 'export PATH=$PATH:LOCATION_OF_NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin' > ~/.bashrc
shouldn't be
Code:
echo 'export PATH=$PATH:LOCATION_OF_NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin' >> ~/.bashrc
with >> instead of >? You don't want to override your complete ~/.bashrc, right?
 
Last edited:
  • Like
Reactions: tuxboy

tuxboy

Member
Dec 19, 2013
48
232
Dhaka
www.adhikary.net
Awesome tutorial! But are you sure echo
Code:
'export PATH=$PATH:LOCATION_OF_NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin' > ~/.bashrc
shouldn't be
Code:
echo 'export PATH=$PATH:LOCATION_OF_NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin' >> ~/.bashrc
with >> instead of >? You don't want to override your complete ~/.bashrc, right?

I just noticed the typo, thanks for pointing it out. Post updated.

Regards
 

_that

Recognized Developer / Inactive RC
Oct 2, 2012
4,821
4,211
In my experience, the native (amd64) readelf worked just as well with ARM binaries. I'm using this command line in my script:

Code:
readelf -d $1 | grep '\(NEEDED\)' | sed -r 's/.*\[(.*)\]/\1/'

The final sed leaves only the bare library names in the output.
 

Ricky Divjakovski

Recognized Developer / Inactive RC
Feb 4, 2013
5,291
7,688
28
Sydney
LEGEND!
just the sort of thing i was looking for!
you should just make binary for this, the "lib dependancy viewer"
it should be called by something like
libdepv PATH/TO/LIB

---------- Post added at 10:24 PM ---------- Previous post was at 10:20 PM ----------

In there any way to use this in windows?

maybe adb shell with busybox installed..
 

jazzespresso

Senior Member
Jul 24, 2012
2,844
1,648
NYC-USA
good one indeed....ok now say how we can find for ported app's missing or shared libs and its dependency? is this also work?
 

_that

Recognized Developer / Inactive RC
Oct 2, 2012
4,821
4,211
you should just make binary for this, the "lib dependancy viewer"
it should be called by something like
libdepv PATH/TO/LIB

Why a binary? Rename the script to libdepv and you can call it already exactly like that. :)

good one indeed....ok now say how we can find for ported app's missing or shared libs and its dependency? is this also work?

For finding native library dependencies of apks, you need to decompile them, search the smali files for invokes of "loadLibrary" and trace the argument back to the string constant (usually it's just a few lines above the loadLibrary call). Prepend "lib" to the string constant and append ".so", that's the name of the required native library.

An example could look like this:

Code:
    const-string v0, "defcontainer_jni"
    invoke-static {v0}, Ljava/lang/System;->loadLibrary(Ljava/lang/String;)V

So we know that this app requires libdefcontainer_jni.so.
 

mikereidis

Inactive Recognized Developer
Jan 28, 2011
7,823
4,146
Ottawa/Gatineau, Canada
:)

I'm using the latest r9d NDK and there are some ld* binaries, but no ldd-arm .

Also my NDK is "Linux 64-bit" so my prebuilt files are under:

$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/

While I'm at it, not all libraries or binaries are explicitly linked to their SO targets. I use dynamic loading in several of mine and so does android for HAL libraries. Best way to find these is by using "strings", assuming there's an explicit string in there, or at least a partial one.
 

digitalhigh

Inactive Recognized Developer
Dec 28, 2010
3,626
11,934
Milwaukee, WI
www.facebook.com
While porting features or ROMs from one Android device to another, some of us frequently encounter missing shared libs error. The process of finding missing shared libraries (.so files) might be a bit trivial, and time consuming. This tutorial is going to demonstrate a one-command way of finding all the dependencies of an executable, or another shared library.

FIRST TIME CONFIGURATION
Open up your terminal, type in:
Code:
$ echo 'readelf -d $1 | grep "\(NEEDED\)" | sed -r "s/.*\[(.*)\]/\1/"' | sudo tee -a /usr/local/bin/ldd-arm
$ sudo chmod +x /usr/local/bin/ldd-arm

[Member @_that told about this (better) way of performing this, without NDK and a more readable output. Thanks!]

FINDING MISSING LIBS

Basic usage is very easy. In your terminal:



Say, for example, we want to find out the dependecies (which shared libraries are required) of my sensor HAL. I would type in terminal,

Code:
$ ldd-arm copybit.msm7x27a.so

And, I get the desired output!

Code:
liblog.so
libutils.so
libcutils.so
libhardware.so
libdl.so
libmemalloc.so
libc.so
libstdc++.so
libm.so

And that's how you can find out the libraries you are missing! If your device uses MIPS, just use the mipsel toolchain instead. (Don't forget to hit Thanks if this helps you!)

I love you so much.

Trying to work out bugs with M8VZW...this might just be what I needed. :D
 

tuxboy

Member
Dec 19, 2013
48
232
Dhaka
www.adhikary.net
:)

I'm using the latest r9d NDK and there are some ld* binaries, but no ldd-arm .

Also my NDK is "Linux 64-bit" so my prebuilt files are under:

$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/

ldd-arm is actually a little command we created at the very beginning of the post.

While I'm at it, not all libraries or binaries are explicitly linked to their SO targets. I use dynamic loading in several of mine and so does android for HAL libraries.

True.
 
  • Like
Reactions: mikereidis

Entropy512

Senior Recognized Developer
Aug 31, 2007
14,088
25,086
Owego, NY
In my experience, the native (amd64) readelf worked just as well with ARM binaries. I'm using this command line in my script:

Code:
readelf -d $1 | grep '\(NEEDED\)' | sed -r 's/.*\[(.*)\]/\1/'

The final sed leaves only the bare library names in the output.

Yep. Same here - my system readelf seems fine when used on ARM binaries for this purpose.

(fairly old trick, but the sed stuff makes it a lot nicer.)

Obviously another approach to this is to take the above command and paste it into a text editor, save it, and chmod +x it

Rather than use /usr/local/bin/ I use ~/bin and add it to my PATH
 

oracle01642

Senior Member
Nov 10, 2012
1,224
277
XDA
Can this be used to port apps? I am wanting to install some S5 apps on my Galaxy S4, but don't know what libs etc are needed. Could this be used to find out?
 

Top Liked Posts

  • There are no posts matching your filters.
  • 52
    While porting features or ROMs from one Android device to another, some of us frequently encounter missing shared libs error. The process of finding missing shared libraries (.so files) might be a bit trivial, and time consuming. This tutorial is going to demonstrate a one-command way of finding all the dependencies of an executable, or another shared library.

    FIRST TIME CONFIGURATION
    Open up your terminal, type in:
    Code:
    $ echo 'readelf -d $1 | grep "\(NEEDED\)" | sed -r "s/.*\[(.*)\]/\1/"' | sudo tee -a /usr/local/bin/ldd-arm
    $ sudo chmod +x /usr/local/bin/ldd-arm

    [Member @_that told about this (better) way of performing this, without NDK and a more readable output. Thanks!]

    FINDING MISSING LIBS

    Basic usage is very easy. In your terminal:

    $ ldd-arm PATH_TO_YOUR_EXECUTABLE_OR_LIBRARY

    Say, for example, we want to find out the dependecies (which shared libraries are required) of my sensor HAL. I would type in terminal,

    Code:
    $ ldd-arm sensors.msm7x27a.so

    And, I get the desired output!

    Code:
    liblog.so
    libutils.so
    libcutils.so
    libhardware.so
    libdl.so
    libmemalloc.so
    libc.so
    libstdc++.so
    libm.so

    And that's how you can find out the libraries you are missing!
    7
    you should just make binary for this, the "lib dependancy viewer"
    it should be called by something like
    libdepv PATH/TO/LIB

    Why a binary? Rename the script to libdepv and you can call it already exactly like that. :)

    good one indeed....ok now say how we can find for ported app's missing or shared libs and its dependency? is this also work?

    For finding native library dependencies of apks, you need to decompile them, search the smali files for invokes of "loadLibrary" and trace the argument back to the string constant (usually it's just a few lines above the loadLibrary call). Prepend "lib" to the string constant and append ".so", that's the name of the required native library.

    An example could look like this:

    Code:
        const-string v0, "defcontainer_jni"
        invoke-static {v0}, Ljava/lang/System;->loadLibrary(Ljava/lang/String;)V

    So we know that this app requires libdefcontainer_jni.so.
    5
    Many thanks mate, you do not know how long I was looking for this :good: so I imagine that if I do not find in Smali invoke of loadLibrary mean this app not need/create any .so file when installed, right?

    If it's not a system app, the apk will contain all required libraries anyway, so you just need to look what's inside the apk. :) For system apps it's different, because all their shared libraries are mixed together in /system/lib. For both system and non-system apps, the native libraries need to be loaded somehow from the compiled Java code, and I think there is no other way than calling loadLibrary. So, yes, if there is no loadLibrary call, the app doesn't use any native libraries. Or it uses an unknown mechanism to load them. :)

    Not directly related to your apk question, but related to the topic of this thread:
    I've recently written a script to check native dynamic library dependencies. It does not only check which libraries are needed, but also if those libraries are compatible and if all unresolved symbols are resolved, and which symbols are used from which library. Very useful for porting ROMs onto older proprietary HAL libraries or for porting stock system apps to different ROMs. If there is interest, I'm going to release it in a separate thread with some documentation. If you need it already now, look for "ldcheck" in my github.
    4
    In my experience, the native (amd64) readelf worked just as well with ARM binaries. I'm using this command line in my script:

    Code:
    readelf -d $1 | grep '\(NEEDED\)' | sed -r 's/.*\[(.*)\]/\1/'

    The final sed leaves only the bare library names in the output.
    3
    What smali to search for? Or do we need to check all the smali?

    All smali files that contain the string "loadLibrary". Use grep or similar.