If android installed at below path:
/system/product/priv-app/mynative/app-debug.apk
And this apk contains a jni library:
app-debug/lib/arm64-v8a/libnative-lib.so
Where is the path android to load the library libnative-lib.so? I search device and never find that so file.
The answer might be: "it never was on the filesystem to begin with".
Modern Android build infrastructure sets extractNativeLibraries=false by default. This keeps the .so in the APK instead of having a copy linger on your device.
EDIT: I did some source digging, the Android runtime linker can open libraries directly from zip files if the filename contains !/. I think this would just show up as another mapping of the APK in /proc/pid/maps, but I did not investigate beyond that (eg how relocation is done in this case or what namespaces have to do with it).
Related
I build a native executable, that runs inference using gpu delegation. This executable relies on the libtensorflowlite_gpu_delegate.so that I copied to the same directory. However, when I run it, I got error saying that this shared lib is not found, so apparently, I need to set the RPATH to ./, and the executable is able to find this shared lib.
But then I ran into another error saying: "libOpenCL.so not found". I found this shared lib in /system/vendor/lib64, so I copied it to the same directory, and everything works.
However, I dont want to duplicate this shared lib if system already have it, so I add RPATH of /system/vendor/lib64 to my executable, and I confirmed with readelf:
Dynamic section at offset 0x9fc0058 contains 40 entries:
Tag Type Name/Value
0x000000000000001d (RUNPATH) Library runpath: [/system/vendor/lib64/:./]
However, with this new RPATH, I still get error saying "libOpenCL.so not found". Not sure if that is access constrain, but I am under adb shell with su enabled. and that file has accesibility of -rw-r--r--
My question is:
why setting RPATH here does not work?
I feel this may not be the best way to let my native app to find the native libs under system folder, I have another non-native app that can use GPU delegate. I read docs saying any native shared library is accessible regardless of whether it is an NDK library at least for the API_LEVEL I have. So I just wonder if there is away to add access for natively built apps?
Because apps are not allowed to access vendor libraries: https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#private-api-enforced-for-api-level-24
Issue
Built APK looks wrong and prevents my application from loading its native library because it cannot find a dependency when calling dlopen(): dlopen failed: library "libboost_filesystem.so.1.68.0" not found.
jniLibs content
My jniLibs directory, for the target platform, contains the following files:
libboost_filesystem.so.1.68.0 is the "real" shared object.
libboost_filesystem.so is a symlink to libboost_filesystem.so.1.68.0
APK Content
After building, the APK contains a libboost_filesystem.so which now is the binary object (not a symlink).
It seems like Android build system followed the symlink, grabbed content of the "pointed-to" file, but used the name of the symlink instead.
I have tried to remove the symlink from the jniLibs folder, but doing that it seems that the xxx.so.VERSION files are then ignored.
Question
How can I embed my "full name" shared object into the jniLibs without the Android build system messing with it ?
No you can't. You should avoid versioning the so file. See https://stackoverflow.com/a/45058227/192373 for instructions.
It's also quite natural that Android does not support this technique, because your native libraries belong to your APK and no version conflict can occur.
Consider linking boost filesystem statically to avoid extra lookup.
I'm trying to debug my Android app (I'm using the libdash library through NDK) as a native application, and I'm having this error:
Error while mapping shared library sections:
`/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/libjpeg.so': not in executable format: File format not recognized
warning: Could not load shared library symbols for 92 libraries, e.g. /system/bin/linker.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
warning: Unable to find dynamic linker breakpoint function.
GDB will retry eventurally. Meanwhile, it is likely
that GDB is unable to debug shared library initializers
or resolve pending breakpoints after dlopen().
this is the library that i'm trying to use
http://www.bitmovin.net/libdash/
http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/
Follow these directions. You need to take the library and put it somewhere special.
Your issue appears to be that you are cross compiling for Android, yet you haven't set your shared library lookup path. The result is that gdb is looking in your host system path to find the shared libraries instead.
Your host system is the system you run the build of your code on (your PC probably). Your target system is the system you're going to run the resulting "executable" on (some Android device). That's why it's called cross compiling. Since the two systems aren't the same, you need to make sure you're using a cross compiled version of gdb (one that knows your target and host types), which was probably provided with the Android SDK, and you also need to make sure you set the search path for the shared libraries.
You should check the general GDB documentation for details, but the method I prefer for setting my cross-compiled library paths is to set the solib-absolute-path. You can do that by running the following from within GDB:
(gdb) set solib-absolute-path <path to root directory all Android shared libraries are under>
This requires that you have the same directory structure as your Android file system with regard to library paths somewhere on your PC.
An alternate and more portable way of doing it is to set the solib-search-path so it will search the Android library locations within the SDK on your PC rather than your PC's default library search path, but for details on that you'll have to read the GDB documentation since I don't use it often enough to help much more.
I was able to follow directions in this question to build a shared lib of openssl for Android.
E.g.
cd openssl-fips-2.0/
./config
make
make install
And
cd openssl-1.0.1c/
./config fips --with-fipsdir=/usr/local/ssl/fips-2.0/ shared
make depend
make
This generates libcrypto.so.1.0.0 and libssl.so.1.0.0 with corresponding symbolic links to them as libcrypto.so and libssl.so.
Since the NDK build system doesn't support versioned shared libraries I had to use the symbolic links (with PREBUILT_SHARED_LIBRARY). However, with this, the libraries end up getting to the device as libcrypto.so and libssl.so instead of as libcrypto.so.1.0.0 and libssl.so.1.0.0 causing my library to fail to load as it is looking for the libraries with the version names.
The linked question mentions loading the libraries with System.load(libcrypto.so.1.0.0) instead of with System.loadLibrary() but I have not been able to get this to work even with full paths since as mentioned earlier, the file is copied to the device as libcrypto.so.
Anyone done this successfully?
Note: I've also tried modifying the openssl-1.0.1c config and makefiles to generate libcrypto.1.0.0.so (e.g. with the version number before the extension in the filename and soname) and this allowed me to get around the previous loading issue. However, with that I get an error when I try to turn on FIPS mode with FIPS_module_mode_set (FIPS_R_FINGERPRINT_DOES_NOT_MATCH).
I don't know yet why that is happening, but it could be due to NDK stripping of 'unneeded' stuff (see this question)... I'm still looking at this as well but if someone has some info on this as well it would be MUCH appreciated.
Let us identify the problem correctly. It's likely not the NDK build that causes problems, and definitely not the linker which strips away unused entries when it builds a shared lib from static lib.
First of all, I am not sure you can deliver FIPS mode in a usual APK, without rebuilding or at least rooting Android (see for example http://gcn.com/articles/2010/12/23/android-fips-security.aspx).
There is no problem for System.load() to load a versioned .so when you a) specify the full path correctly (e.g. System.load("/data/local/tmp/libssl.so.1.0.0")) and b) the file is delivered to that path. For the first tests, I would suggest to manually upload libcrypto.so.1.0.0 and libssl.so.1.0.0 to /sdcard/ and see if FIPS fingerprint becomes happier.
If the location on /sdcard/ causes any problem, you can try /data/local/ or /data/local/tmp/. You can also use /data/data/(your package)/files/. The latter has one advantage: it will be automatically deleted by the system when your app is uninstalled.
To make a versioned .so (like libcrypto.so.1.0.0) part of your APK, copy it to the assets folder of your project. It will be responsibility of your Java code to copy it from there to the designated location on disk. Make sure this Java code handles correctly upgrades and SD card swaps.
I am attempting to add a third-party library to my Android app. It utilizes a .jar file and a .so file. This is a pre-built .so file (i.e. not built specifically for the Android app) - which I know will provide me with plenty of problems down the road. I do NOT have access to the source files for the .jar or .so files!
I am attempting to dynamically load the library via a call to System.loadLibrary("foo");. Currently, when attempting to run the app, it crashes with the UnsatisfiedLinkError: Library foo not found. I have the .so file in both the libs/ and the libs/armeabi file in my project.
Am I chasing after a ghost here? I am trying to determine if what I'm after is even feasible. I have no native code that I'm referencing - all my function calls are to the .jar file that is, as I understand it, backed by the .so file. I have not run the Android.mk file as I'm not compiling anything - the .so file is already provided. I could really use some advice as to what direction to proceed from here.
It's possible that the base name given to System.loadLibrary() is expanding to a file (or path) name different than that of the actual prebuilt library. Logcat should show you exactly what it is trying to load. You could also use System.load() with a full path/file name instead of System.loadLibrary() - though you really should get it working with the later.
While I think it would generate a different error message, it's also possible that the .so is not android compatible - it must not only be for a compatible processor type and abi, but not depend on any dynamic libraries (such as a non-bionic libc) not installed on the device.