title: android load library failed
body: <p>When I use <code>System.loadLibrary()</code> to load my so file, rarely, it fails and the Logcat says </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>After searching the Internet, I don't find any infomation about </p>
  
  <pre><code>cannot locate '__cxa_atexit'
  </code></pre>
  
  <p>(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.</p>
  
  <p>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'.</p>
  
  <p>This problem may be the same as <a href="https://stackoverflow.com/questions/32796782/loading-android-so-sometimes-fails-rarely?lq=1">another problem</a>.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>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.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>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 <a href="https://github.com/android/platform_bionic/blob/android-sdk-4.0.3_r1/linker/linker.c" rel="nofollow noreferrer">runtime dlopen</a> -> find_library -> init_library -> link_image -> reloc_library.</p>
  
  <p>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:</p>
  
  <pre><code>DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
  </code></pre>
  
  <p>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 ?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>It crashes on ALL Android versions, not only 4.0.3 &amp; 4.0.4.</p>
  
  <p>Error message on 4.0.3 &amp; 4.0.4 is</p>
  
  <pre><code>java.lang.UnsatisfiedLinkError: Cannot load library: reloc_library[1286]: 121 cannot locate '__cxa_atexit'
  </code></pre>
  
  <p>as mentioned above.</p>
  
  <p>When loading some other so on 4.0.3 &amp; 4.0.4, it is</p>
  
  <pre><code>cannot locate 'strcpy'
  </code></pre>
  
  <p>Error message on 4.2.2 is</p>
  
  <pre><code>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
  </code></pre>