error: only position independent executables (PIE) are supported

PIE, position independent executables.

Android 4.1上开始支持PIE,所以4.1之前的版本不能使用PIE的executable,而之前开发的app一般都是非PIE的。

Android 4.1

        PIE (Position Independent Executable) support
        Read-only relocations / immediate binding (-Wl,-z,relro -Wl,-z,now)
        dmesg_restrict enabled (avoid leaking kernel addresses)
        kptr_restrict enabled (avoid leaking kernel addresses)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在android L后,linker.cpp中,

//bionic\linker\linker.cpp
static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
  ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
  //不是PIE的直接报错返回,而之前的版本中都没有
  if (elf_hdr->e_type != ET_DYN) {
    __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
    exit(EXIT_FAILURE);
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

所以在android L后,非PIE的exe执行都会报错error: only position independent executables (PIE) are supported.

1.但是如果在android 4.1之前的版本运行PIE, 也是有办法的: 
The Chromium project released a wrapper that allows PIE binaries to run on pre-JB Android releases。

具体的使用方法见http://stackoverflow.com/questions/24818902/running-a-native-library-on-android-l-error-only-position-independent-executab

其中run_pie在android L中的external\chromium_org\tools\android\run_pie中可找到,下面是run_pie.c中的注释,说明run_pie主要是为了在linker不支持PIE的早期版本中运行PIE。

// This is a wrapper to run position independent executables on Android ICS,
// where the linker doesn't support PIE. This requires the PIE binaries to be
// built with CFLAGS +=-fvisibility=default -fPIE, and LDFLAGS += -rdynamic -pie
// such that the main() symbol remains exported and can be dlsym-ed.
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

2.那么在android L之后的版本运行非PIE,有没有办法? 
目前是没有解决方案,除非你把bionic的linker替换,具体参考下面的文章,使用其中修改编译过的linker, http://forum.xda-developers.com/google-nexus-5/development/fix-bypassing-pie-security-check-t2797731


LOCAL_PATH := $(call my-dir)

# Forcefully disable PIE globally. This makes it possible to
# build some binaries without PIE by adding the necessary flags
# manually. These will not get reset by $(CLEAR_VARS). PIE is
# force-enabled on NDK 10b so we'll need this even if APP_PIE
# is set to false.
TARGET_PIE := false
NDK_APP_PIE := false

include $(CLEAR_VARS)

# Enable PIE manually. Will get reset on $(CLEAR_VARS). This
# is what enabling PIE translates to behind the scenes.
LOCAL_CFLAGS += -fPIE
LOCAL_LDFLAGS += -fPIE -pie

LOCAL_MODULE := mymod

LOCAL_SRC_FILES := \
    mymod.c

include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)

LOCAL_MODULE := mymod-nopie

LOCAL_SRC_FILES := \
    mymod.c

include $(BUILD_EXECUTABLE)




发布了18 篇原创文章 · 获赞 34 · 访问量 27万+

猜你喜欢

转载自blog.csdn.net/brandon2015/article/details/70805085