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:     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初始化-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)

试图在 Alpine 下编译supermin,但是src/Makefile.am#L159
require init是通过file命令静态链接的。

最佳答案

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

strace下运行程序还将验证程序启动时未加载任何共享对象。

关于c++ - 为什么文件命令报告动态链接到gcc -static,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46882179/

10-11 18:51