为什么文件命令报告动态链接到gcc -static

时间:2017-10-23 04:55:25

标签: c++ gcc makefile alpine

gcc -static -g -O2 -static -o init init-init.o 
file init
# init: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, with debug_info, not stripped
ldd init
#    ldd (0x7fd49e2ed000)

objdump -p init

init:     file format elf64-x86-64

Program Header:
    LOAD off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**21
        filesz 0x00000000000076e4 memsz 0x00000000000076e4 flags r-x
    LOAD off    0x0000000000007e30 vaddr 0x0000000000207e30 paddr 0x0000000000207e30 align 2**21
        filesz 0x00000000000002d8 memsz 0x0000000000001488 flags rw-
DYNAMIC off    0x0000000000007e60 vaddr 0x0000000000207e60 paddr 0x0000000000207e60 align 2**3
        filesz 0x0000000000000150 memsz 0x0000000000000150 flags rw-
STACK off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**4
        filesz 0x0000000000000000 memsz 0x0000000000000000 flags rw-
RELRO off    0x0000000000007e30 vaddr 0x0000000000207e30 paddr 0x0000000000207e30 align 2**0
        filesz 0x00000000000001d0 memsz 0x00000000000001d0 flags r--

Dynamic Section:
SYMBOLIC             0x0000000000000000
INIT                 0x00000000000002c0
FINI                 0x0000000000006473
GNU_HASH             0x0000000000000158
STRTAB               0x00000000000001b0
SYMTAB               0x0000000000000180
STRSZ                0x0000000000000007
SYMENT               0x0000000000000018
DEBUG                0x0000000000000000
PLTGOT               0x0000000000207fb0
RELA                 0x00000000000001b8
RELASZ               0x0000000000000108
RELAENT              0x0000000000000018
BIND_NOW             0x0000000000000000
FLAGS_1              0x0000000008000001
RELACOUNT            0x000000000000000b

readelf init -h

ELF Header:
Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
Class:                             ELF64
Data:                              2's complement, little endian
Version:                           1 (current)
OS/ABI:                            UNIX - System V
ABI Version:                       0
Type:                              DYN (Shared object file)
Machine:                           Advanced Micro Devices X86-64
Version:                           0x1
Entry point address:               0x1158
Start of program headers:          64 (bytes into file)
Start of section headers:          308248 (bytes into file)
Flags:                             0x0

为什么Type: DYN (Shared object file)

尝试在高山下编译supermin,但src/Makefile.am#L159 require initfile命令静态链接。

1 个答案:

答案 0 :(得分:2)

ET_DYN用于与位置无关的可执行文件(PIE),无论它们是否静态链接。动态部分缺少程序解释器和DT_NEEDED条目表明程序确实是静态链接的。您可以使用readelf -l(无.interp)和readelf -d(无NEEDED)进行检查。

strace下运行程序还将验证程序启动时是否没有加载共享对象。