GCC: -static and -pie are incompatible for x86?

前端 未结 3 1449
暗喜
暗喜 2021-02-15 15:47

I\'m recompiling some executable for Android 5.0 as it requires executables to be PIE. I was able to recompile it for ARM with just adding some argumen

相关标签:
3条回答
  • 2021-02-15 16:12

    I just did quick test with the fallowing in te.c:

    int main( int argc, const char* argv[] )
    {
       return 0;
    }
    

    Running arm-linux-androideabi-gcc -o conftest -static -FPIE -pie te.c produces no error. However file -k conftest outputs

    conftest: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), not stripped
    

    readelf -l conftest outputs Elf file type is DYN (Shared object file) Entry point 0x500 There are 7 program headers, starting at offset 52

    Program Headers:
      Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
      PHDR           0x000034 0x00000034 0x00000034 0x000e0 0x000e0 R   0x4
      INTERP         0x000114 0x00000114 0x00000114 0x00013 0x00013 R   0x1
          [Requesting program interpreter: /system/bin/linker]
    ...
    

    The presence of the PHDR and INTERP headers indicates that -pie silently overrides -static in the arm compiler. Why this is I don't know but I would consider it a bug that no warning is given when -static and -pie are used together. Instead programmers like you are left with the false impression that the two options can be used to together on arm.

    Just to clarify the only behavioral difference here is that the x86 compiler errors on seeing both --static and --pie whereas the arm version silently ignores --static if --pie is given. If only one these is given the behavior is the same for both compilers.

    0 讨论(0)
  • 2021-02-15 16:23

    If -pie and -static are both given together, gcc will issue unexpected error.

    -pie

    Produce a position independent executable on targets that support it. For predictable results, you must also specify the same set of options used for compilation (-fpie, -fPIE, or model suboptions) when you specify this linker option.

    -pie actually create a DYN type elf file with INTERP with /system/bin/linker

    executable compiled with -pie

    -static

    On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.

    -static create a EXEC type elf file with no INTERP

    0 讨论(0)
  • 2021-02-15 16:29

    Google's NDK tool contain some info about PIE usage. Visit build/core/build-binary.mk, see line 209. It says:

    # enable PIE for executable beyond certain API level, unless "-static"

    I guess, it is linux dynamic link principle's limit. Because Android interpreter (/system/bin/linker) determining which address elf file to be loaded in a static linked file has no interpreter, elf file will be mapped to memory into a fixed address by linux kernel. Here is a discuss about this change Google issue

    If I have any mistake please figure it out:)

    0 讨论(0)
提交回复
热议问题