我在linux中有一个二进制文件。如何检查是否已使用“-static”编译?
答案 0 :(得分:20)
ldd /path/to/binary
不应列出任何共享库。
答案 1 :(得分:10)
您也可以使用file
命令(objdump
也可能有用)。
答案 2 :(得分:2)
检查其是否具有类型为INTERP
的程序标头
在较低级别,如果可执行文件没有类型为program header的静态文件,则它是静态的:
Elf32_Phd.p_type == PT_INTERP
System V ABI spec中已提及。
请记住,程序头决定ELF segments,包括类型PT_LOAD
的那些将被加载到内存中并运行。
如果存在该标头,则其内容正是动态加载程序的路径。
readelf
检查
我们可以用readelf
来观察。首先动态地编译一个C hello世界:
gcc -o main.out main.c
然后:
readelf --program-headers --wide main.out
输出:
Elf file type is DYN (Shared object file)
Entry point 0x1050
There are 11 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000040 0x0000000000000040 0x0000000000000040 0x000268 0x000268 R 0x8
INTERP 0x0002a8 0x00000000000002a8 0x00000000000002a8 0x00001c 0x00001c R 0x1
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x000560 0x000560 R 0x1000
LOAD 0x001000 0x0000000000001000 0x0000000000001000 0x0001bd 0x0001bd R E 0x1000
LOAD 0x002000 0x0000000000002000 0x0000000000002000 0x000150 0x000150 R 0x1000
LOAD 0x002db8 0x0000000000003db8 0x0000000000003db8 0x000258 0x000260 RW 0x1000
DYNAMIC 0x002dc8 0x0000000000003dc8 0x0000000000003dc8 0x0001f0 0x0001f0 RW 0x8
NOTE 0x0002c4 0x00000000000002c4 0x00000000000002c4 0x000044 0x000044 R 0x4
GNU_EH_FRAME 0x00200c 0x000000000000200c 0x000000000000200c 0x00003c 0x00003c R 0x4
GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x10
GNU_RELRO 0x002db8 0x0000000000003db8 0x0000000000003db8 0x000248 0x000248 R 0x1
Section to Segment mapping:
Segment Sections...
00
01 .interp
02 .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt
03 .init .plt .plt.got .text .fini
04 .rodata .eh_frame_hdr .eh_frame
05 .init_array .fini_array .dynamic .got .data .bss
06 .dynamic
07 .note.ABI-tag .note.gnu.build-id
08 .eh_frame_hdr
09
10 .init_array .fini_array .dynamic .got
因此请注意,INTERP
标头在那里,readelf
甚至提供了它的短28(0x1c)字节内容的快速预览:/lib64/ld-linux-x86-64.so.2
,这一点非常重要。动态加载程序的路径(27个字节长,\0
为1)。
请注意,它如何与其他细分受众群(包括例如那些实际加载到内存中的文件,例如:.text
。
然后我们可以使用以下命令直接提取那些字节而无需预览:
readelf -x .interp main.out
给出:
Hex dump of section '.interp':
0x000002a8 2f6c6962 36342f6c 642d6c69 6e75782d /lib64/ld-linux-
0x000002b8 7838362d 36342e73 6f2e3200 x86-64.so.2.
如How can I examine contents of a data section of an ELF file on Linux?
所述 file
源代码
file
在src/readelf.c的5.36源代码注释中,它还检查PT_INTERP
:
/*
* Look through the program headers of an executable image, searching
* for a PT_INTERP section; if one is found, it's dynamically linked,
* otherwise it's statically linked.
*/
private int
dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
int num, size_t size, off_t fsize, int sh_num, int *flags,
uint16_t *notecount)
{
Elf32_Phdr ph32;
Elf64_Phdr ph64;
const char *linking_style = "statically";
在消息git grep statically
中与main.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
一起找到。
但是,与代码相比,此注释似乎过时了,而代码会检查PT_DYNAMIC
:
case PT_DYNAMIC:
linking_style = "dynamically";
doread = 1;
break;
我不确定为什么要这样做,现在我很懒惰地研究git log
。特别是,当我尝试使用--no-dynamic-linker
制作静态链接的PIE可执行文件时,这让我有些困惑,如以下所示:How to create a statically linked position independent executable ELF in Linux?,它没有PT_INTERP
但确实有PT_DYNAMIC
,而且我不希望使用动态加载器。
Linux内核源代码
Linux内核5.0在fs/binfmt_elf.c的exec系统调用期间读取ELF文件,如以下说明所述:How does kernel get an executable binary file running under linux?
内核在load_elf_binary
的程序头上循环
for (i = 0; i < loc->elf_ex.e_phnum; i++) {
if (elf_ppnt->p_type == PT_INTERP) {
/* This is the program interpreter used for
* shared libraries - for now assume that this
* is an a.out format binary
*/
我还没有完全阅读代码,但是我希望只有在找到INTERP
的情况下,它才会使用动态加载程序,否则应该使用哪个路径?
PT_DYNAMIC
。
奖金:检查是否使用了-pie
我已在Why does GCC create a shared object instead of an executable binary according to file?
处对此进行了详细说明。