1

Just build a simple "helloworld" using android ndk, but I got shard object which I supposed to get executable file. And after I pushed this file to my arm emulator, I got a segmentation fault, but real device is OK. What's the problem? Here is my os version:

Darwin avator 13.4.0 Darwin Kernel Version 13.4.0: Sun Aug 17 19:50:11 PDT 2014; root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64 i386 MacBookPro11,1 Darwin

Here is my Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := test-libstl
LOCAL_SRC_FILES := test-libstl.cpp
include $(BUILD_EXECUTABLE)

And after ndk-build,I got the file:

$file ../libs/arm64-v8a/test-libstl
../libs/arm64-v8a/test-libstl: ELF 64-bit LSB shared object, version 1 (SYSV), dynamically linked (uses shared libs), stripped
matsjoyce
  • 5,744
  • 6
  • 31
  • 38
jiych.guru
  • 664
  • 1
  • 5
  • 20

2 Answers2

2

This is not an error in itself - it's only the file utility which interprets a position independent executable (PIE) as a shared object - your executable has been built just fine.

Only Android 4.1 and newer supports PIE executables, and on 5.0, non-PIE executables aren't allowed any longer - this may be why you can't run it in the emulator. See Running a native library on Android L. error: only position independent executables (PIE) are supported for more details on this issue. If your target platform in the NDK is Android 4.1 or newer (android-16), executables will be built with PIE enabled.

Community
  • 1
  • 1
mstorsjo
  • 12,983
  • 2
  • 39
  • 62
  • this really helps me,my emulator's version is 4.0,and real device is 4.4.Just another two questions:1.Is PIE a must on android 4.1 and newer? 2.I got another file which can run on my emulator 4.0,and `file` shows: `ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), stripped`,and i'm a little confused about this.Is this file PIE? thks. – jiych.guru Dec 16 '14 at 10:32
  • No, PIE is not a must on android 4.1, but on 5.0 it is a requirement. The second file, which `file` says is an executable, is not PIE, and will not run on 5.0. – mstorsjo Dec 16 '14 at 10:39
  • how can i build non-PIE executable that can run on Android 4.0?I try to set `APP_PIE := false` and `APP_PLATFORM := android-9` in `Application.mk`,but it doesnt take effect.And my ndk is r10. – jiych.guru Dec 16 '14 at 17:22
  • It seems that setting `APP_PIE := false` doesn't have any effect - you can only use `APP_PIE := true` to enable it for the case when it isn't already enabled. But `APP_PLATFORM := android-9` in `Application.mk` should indeed work - it does work when I try it out locally. Are you sure that `Application.mk` is placed correctly (in the `jni` subdirectory)? Try adding `NDK_LOG=1` at the end of your `ndk-build` call to get more logging which may give clues to what's happening. – mstorsjo Dec 16 '14 at 22:10
  • `Application.mk` is where it should,and add `NDK_LOG=1` give something relative like this:`Android NDK: APP_PIE is false Android NDK: Application local targets unknown platform 'android-9' Android NDK: Switching to android-L`. – jiych.guru Dec 17 '14 at 02:39
  • and i use `android list`,there's no `android-9` available target on my machine indeed.But there's `android-14` available,when i used `APP_PLATFORM := android-14` in `Application.mk`,`ndk-build` got something like above,`Android NDK: APP_PIE is false Android NDK: Application local targets unknown platform 'android-14' Android NDK: Switching to android-L`. – jiych.guru Dec 17 '14 at 02:56
  • got it resolved.My `ndk` is r10,which only contains `android-L` platform.I've already download the latest `r10d` right now.After added `APP_PIE := false` in `Application.mk`,i got non-PIE executable file.It's interesting that `APP_PLATFORM := xxx` doesn't take effect in `r10d` now:) – jiych.guru Dec 17 '14 at 05:48
0

try to use LOCAL_LDFLAGS := -static

it help me get a ELF 64-bit LSB executable file.

SDJSK
  • 1,292
  • 17
  • 24