Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking STL into a standalone Android NDK executable

The following code causes a segmentation fault after printing the string:

#include <iostream>
using namespace std;

int main()
{
    cout << "ndktest" << endl;
    return 0;
}
  • If I change cout to printf, it doesn't crash.
  • If I call cout in a different function other than main, and that function is never accessed, it still crashes.
  • It crashes on Froyo, but not ICS.

For reference, here's my Android.mk:

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := ndktest
LOCAL_SRC_FILES := main.cpp

include $(BUILD_EXECUTABLE)

and Application.mk:

APP_STL := stlport_static

Here's the crash from logcat:

I/DEBUG   (  872): pid: 4234, tid: 4234  >>> /data/ndktest <<<
I/DEBUG   (  872): signal 11 (SIGSEGV), fault addr 00024004
I/DEBUG   (  872):  r0 00033824  r1 00001000  r2 00024005  r3 bea42cfc
I/DEBUG   (  872):  r4 40008090  r5 0000000a  r6 40008000  r7 afd42328
I/DEBUG   (  872):  r8 00000000  r9 00000000  10 00000000  fp 00000000
I/DEBUG   (  872):  ip 40008084  sp bea42ce8  lr afd192dd  pc 00024004  cpsr 60000030
I/DEBUG   (  872):  d0  0000000000000000  d1  0000000000000000
I/DEBUG   (  872):  d2  0000000000000000  d3  0000000000000000
I/DEBUG   (  872):  d4  0000000000000000  d5  0000000000000000
I/DEBUG   (  872):  d6  0000000000000000  d7  0000000000000000
I/DEBUG   (  872):  d8  0000000000000000  d9  0000000000000000
I/DEBUG   (  872):  d10 0000000000000000  d11 0000000000000000
I/DEBUG   (  872):  d12 0000000000000000  d13 0000000000000000
I/DEBUG   (  872):  d14 0000000000000000  d15 0000000000000000
I/DEBUG   (  872):  scr 00000000
I/DEBUG   (  872): 
I/DEBUG   (  872):          #00  pc 00024004  
I/DEBUG   (  872):          #01  pc 000192da  /system/lib/libc.so
I/DEBUG   (  872):          #02  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #03  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #04  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #05  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #06  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #07  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #08  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #09  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #10  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #11  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #12  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #13  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #14  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #15  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #16  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #17  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #18  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #19  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #20  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #21  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #22  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #23  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #24  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #25  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #26  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #27  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #28  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #29  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #30  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872):          #31  pc 0001972c  /system/lib/libc.so
I/DEBUG   (  872): 
I/DEBUG   (  872): code around pc:
I/DEBUG   (  872): 00023fe4 ffffffff ffffffff ffffffff ffffffff 
I/DEBUG   (  872): 00023ff4 ffffffff ffffffff ffffffff ffffffff 
I/DEBUG   (  872): 00024004 ffffffff ffffffff ffffffff ffffffff 
I/DEBUG   (  872): 00024014 ffffffff ffffffff ffffffff ffffffff 
I/DEBUG   (  872): 00024024 ffffffff ffffffff ffffffff ffffffff 
I/DEBUG   (  872): 
I/DEBUG   (  872): code around lr:
I/DEBUG   (  872): afd192bc f8cde018 f7f4e00c b930ec56 0c0cf844 
I/DEBUG   (  872): afd192cc 46302201 f7f49900 9803ec4e 47909a05 
I/DEBUG   (  872): afd192dc f1153c0c d5d735ff 2e006836 463cd1ce 
I/DEBUG   (  872): afd192ec b9b99901 c03cf8df 300cf857 f8471e58 
I/DEBUG   (  872): afd192fc b978000c 587a490c e0046810 68059900 
I/DEBUG   (  872): 
I/DEBUG   (  872): stack:
I/DEBUG   (  872):     bea42ca8  b000c9b8  
I/DEBUG   (  872):     bea42cac  00000014  
I/DEBUG   (  872):     bea42cb0  b00093e0  /system/bin/linker
I/DEBUG   (  872):     bea42cb4  b001e9b8  
I/DEBUG   (  872):     bea42cb8  b000c9b8  
I/DEBUG   (  872):     bea42cbc  b0001c78  /system/bin/linker
I/DEBUG   (  872):     bea42cc0  b001e724  
I/DEBUG   (  872):     bea42cc4  00000005  
I/DEBUG   (  872):     bea42cc8  b000c9b8  
I/DEBUG   (  872):     bea42ccc  b0001de3  /system/bin/linker
I/DEBUG   (  872):     bea42cd0  b000c9b8  
I/DEBUG   (  872):     bea42cd4  b0002c77  /system/bin/linker
I/DEBUG   (  872):     bea42cd8  00000000  
I/DEBUG   (  872):     bea42cdc  b001ffe8  
I/DEBUG   (  872):     bea42ce0  df002777  
I/DEBUG   (  872):     bea42ce4  e3a070ad  
I/DEBUG   (  872): #01 bea42ce8  00001000  
I/DEBUG   (  872):     bea42cec  00000000  
I/DEBUG   (  872):     bea42cf0  bea42cfc  
I/DEBUG   (  872):     bea42cf4  00033824  
I/DEBUG   (  872):     bea42cf8  00000000  
I/DEBUG   (  872):     bea42cfc  00024005  
I/DEBUG   (  872):     bea42d00  00033824  
I/DEBUG   (  872):     bea42d04  00000000  
I/DEBUG   (  872):     bea42d08  00000000  
I/DEBUG   (  872):     bea42d0c  00000000  
I/DEBUG   (  872):     bea42d10  b000c9b8  
I/DEBUG   (  872):     bea42d14  00000000  
I/DEBUG   (  872):     bea42d18  00000000  
I/DEBUG   (  872):     bea42d1c  afd19731  /system/lib/libc.so
I/BootReceiver(  946): Copying /data/tombstones/tombstone_09 to DropBox (SYSTEM_TOMBSTONE)

Running this through the stack trace tool didn't yield any symbolic information.

Update:

The pc value in the crash dump (0001972c in /system/lib/libc.so) is in an exit routine, which is consistent with crashing after the string is printed:

00019728 <exit>:
   19728:   4604        mov r4, r0
   1972a:   2000        movs    r0, #0
   1972c:   f7ff fd8c   bl  19248 <__cxa_finalize>
   19730:   4620        mov r0, r4
   19732:   f7f3 ee96   blx d460 <_exit>
   19736:   bf00        nop
like image 562
Ravi Avatar asked Jul 06 '12 21:07

Ravi


People also ask

What is standalone toolchain?

A toolchain which is ready to use with all the configuration that is the system headers and libraries in the correct path . For Android it will also have the API headers in the path where the toolchain can look it up. Why the term "standalone"?

What compiler does Android NDK use?

Code written in C/C++ can be compiled to ARM, or x86 native code (or their 64-bit variants) using the Android Native Development Kit (NDK). The NDK uses the Clang compiler to compile C/C++.

What is NDK Sysroot?

A sysroot is a directory containing the system headers and libraries for your target. To define the sysroot, you must must know the Android API level you want to target for native support; available native APIs vary by Android API level.


1 Answers

This is probably due to the fact that you use stlport_static instead of stlport_shared.

In the NDK docs (CPLUSPLUS-SUPPORT.html included in the package), this behaviour is described. This unpredicted behaviour derives from the fact that if the application links against two (or more) shared libraries, and both link against stlport_static, some symbols may be duplicated, leading to incorrect behaviour.

like image 178
Sdra Avatar answered Nov 15 '22 11:11

Sdra