问题
When I use System.loadLibrary()
to load my so file, rarely, it fails and the Logcat says
Cannot load library: reloc_library[1286]: 121 cannot locate '__cxa_atexit'
java.lang.UnsatisfiedLinkError: Cannot load library: reloc_library[1285]: 169 cannot locate '__cxa_atexit'...
at java.lang.Runtime.loadLibrary(Runtime.java:370)
at java.lang.System.loadLibrary(System.java:535)
After searching the Internet, I don't find any infomation about
cannot locate '__cxa_atexit'
(especially the key word __cxa_atexit). Why cannot locate this function? This function seems to be in libc.so. I don't use C++ in my native code, only C. My NDK version is android-ndk-r10e. I think "cannot locate __cxa_atexit" maybe a relative clue.
Most of the time (maybe billions starts of app), it can work well, but rarely crashes as above. In other words, I cannot make it crash on my testing phones, however, it will crash rarely on some users'.
This problem may be the same as another problem.
UPDATE
Most of the phones which this crash happens on are android 4.0.3 and android 4.0.4. These two version are both API-15.
UPDATE
After reading some Android's source code, I found this issue may be related to dlopen. The error message "Cannot load library: reloc_library..." comes from the function dlopen which is hijacked at runtime. The trace is runtime dlopen -> find_library -> init_library -> link_image -> reloc_library.
Maybe when it resolve symbols in my so files, it finds the "__cxa_atexit" is undefined. Then it looks up in the loaded symbols, but find nothing. (why cannot find __cxa_atexit ?) Finally it runs to the line 1285, with the code:
DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
I don't know something about linker. Could anyone explain or guess why the __cxa_atexit cannot be located ? Is it a bug of Android ?
UPDATE
It crashes on ALL Android versions, not only 4.0.3 & 4.0.4.
Error message on 4.0.3 & 4.0.4 is
java.lang.UnsatisfiedLinkError: Cannot load library: reloc_library[1286]: 121 cannot locate '__cxa_atexit'
as mentioned above.
When loading some other so on 4.0.3 & 4.0.4, it is
cannot locate 'strcpy'
Error message on 4.2.2 is
java.lang.UnsatisfiedLinkError: Cannot load library: load_library(linker.cpp:767): can't read file /mnt/asec/app-name-1/lib/libname.so: I/O error
回答1:
Firstly, please make sure that you called System.loadLibrary() like below:
public class MainActivity extends Activity {
static {
System.loadLibrary("main")
}
...
}
Then, according to the second answer in this post, the problem was with some external dependencies that were loaded statically in your native library. They were only hanging on Android 4.0 and it was running fine 4.2 and up.So you should check your so file.
I just put his answer here:
if you have the problem, put a __android_log_print in the JNI_onLoad of your library (if you have one). If it is not called, check all the functions that can be called statically (beware, some might be hidden behind macros) and try to remove them to see if your able to load the library
来源:https://stackoverflow.com/questions/32860294/android-load-library-failed