0

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
Community
  • 1
  • 1
homelesser
  • 45
  • 1
  • 6
  • did you see http://stackoverflow.com/questions/32425865/race-condition-in-android-dlopen/32460683#32460683 ? – 18446744073709551615 Sep 30 '15 at 11:18
  • They are not the same problems. I also use "System.loadlibrary()" as you metioned. and if it fails, I will unzip a so file from the apk, and reload it. Most of the time, it can work well, but sometimes crashes. In other words, I cannot make it crash on my own phone, however, it crashes on other users' who use my app. – homelesser Oct 08 '15 at 01:43
  • try this :http://stackoverflow.com/questions/15204492/system-loadlibrary-error – KishuDroid Oct 09 '15 at 06:41
  • @KishuDroid They are not similar problems. My app can work well on most phones without other errors and crashes. – homelesser Oct 12 '15 at 02:41
  • I have never experienced this problem, but it is quite possible that native loader on some 4.0.x devices is not stable enough. One bug in that loader made a fool of itself when both **armeabi** and **armeabi-v7a** variants of the libraries were available. Anyways, one cure would be to add `try … catch` around **System.loadLibrary("main")** and retry. Another cure - to explicitly call **System.loadLibrary("c")** before **main**. – Alex Cohn Oct 15 '15 at 13:22
  • @AlexCohn I agree with u very much. : ) – homelesser Oct 16 '15 at 03:29

1 Answers1

-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

Community
  • 1
  • 1
SHICONG CAO
  • 219
  • 3
  • 7
  • It will crash rarely if I call System.loadLibrary() like yours. I tried another way. When I construct a single instance I call System.loadLibrary(), if failure comes, I unzip a so file from the apk file, and call System.load(). However it is no help. By the way, it crashes on many versions of android os(4.0.3 - 5.1), and easier on 4.0.3 & 4.0.4 comparatively. – homelesser Oct 09 '15 at 06:18
  • I update the question. Any idea about the __cxa_atexit ? – homelesser Oct 09 '15 at 06:33
  • Since you can load the library in most case and crash rarely, I suggest you to debug your so library(if you have the source code), some external dependencies loaded in your so library may cause the issue – SHICONG CAO Oct 09 '15 at 06:36