How to create a statically linked position independent executable ELF in Linux?

后端 未结 1 1084
盖世英雄少女心
盖世英雄少女心 2020-12-20 21:57

I have a working position independent Linux freestanding x86_64 hello world:

main.S

.text
.global _start
_start:
asm_main_after_prologue:
    /* Writ         


        
相关标签:
1条回答
  • 2020-12-20 22:36

    You want to add --no-dynamic-linker to your link command:

    $ ld main.o -o main.out -pie --no-dynamic-linker
    
    $ file main.out
    main.out: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, not stripped
    
    $ ./main.out
    hello
    

    so I guess when I achieve what I want, I will have a valid interpreter, and I'm so going to use my own minimal hello world as the interpreter of another program.

    I am not sure I understood what you are saying correctly. If you meant that main.out would have itself as its interpreter, that's wrong.

    P.S. GLIBC-2.27 added support for -static-pie, so you no longer have to resort to assembly to get a statically linked PIE binary. But you'll have to use very recent GCC and GLIBC.

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