linux – ELF可执行问题
我在
linux上有一些关于ELF可执行文件的奇怪问题.
这是我的系统(uname -a): Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt20-1+deb8u2 (2016-01-02) x86_64 GNU/Linux 我有以下程序(test.asm),我使用NASM组装它: ; program just exits with code 0 using linux INT 80H SECTION .data SECTION .text GLOBAL _start _start: MOV EAX,1 XOR EBX,EBX INT 0x80 我创建三个不同的可执行文件: nasm -f elf32 -o test32-i386.o test.asm ld -m elf_i386 -o test32-i386 test32-i386.o nasm -f elfx32 -o test32-x86_64.o test.asm ld -m elf32_x86_64 -o test32-x86_64 test32-x86_64.o nasm -f elf64 -o test64-x86_64.o test.asm ld -m elf_x86_64 -o test64-x86_64 test64-x86_64.o 这是file命令的输出: test32-i386: ELF 32-bit LSB executable,Intel 80386,version 1 (SYSV),statically linked,not stripped test32-x86_64: ELF 32-bit LSB executable,x86-64,not stripped test64-x86_64: ELF 64-bit LSB executable,not stripped 我感觉合理.但是,运行它们会带来麻烦. > ./test32-i386:没问题,运行正常. 此外,Valgrind产生……有趣的结果. > valgrind ./test32-i386:好的 所以,总结一下: 问题1:为什么Valgrind在运行./test64-x86_64时会引发SIGILL,即使程序在没有Valgrind的情况下似乎工作正常? 问题2:为什么我不能运行./test32-x86_64? Valgrind为该二进制文件提供的错误非常模糊…… 解决方法
对于问题1:有一个针对valgrind的错误,它不支持
int80 instruction in x86_64.我能够在我自己的valgrind(v3.11.0)下重现这一点,并且从浏览源看起来好像它不受支持.
对于问题2:ELF加载程序不支持文件类型.为了在Linux上提供32位二进制文??件的兼容性,它必须在尝试执行二进制文件时对二进制文件进行一些检查. 当我们在test32-x86_64上使用readelf时,它会显示一个标题: ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement,little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: Advanced Micro Devices X86-64 Version: 0x1 Entry point address: 0x400060 Start of program headers: 52 (bytes into file) Start of section headers: 288 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 1 Size of section headers: 40 (bytes) Number of section headers: 5 Section header string table index: 2 即类为32位,机器类型为x86_64.即它是x32 ABI二进制文件 问题是这需要你的内核配置CONFIG_X86_X32_ABI,否则你将失败foul of the check: #define compat_elf_check_arch(x) (elf_check_arch_ia32(x) || (IS_ENABLED(CONFIG_X86_X32_ABI) && (x)->e_machine == EM_X86_64)) 它只支持没有配置选项的32位二进制文??件.如果您有内核选项,则设置此选项:CONFIG_X86_X32 = y和CONFIG_X86_X32_DISABLED未设置(这是我正在查看的linux内核4.3源代码). 因此,您需要使用此支持配置的内核来运行代码 – perror之所以没有看到问题是他的内核似乎是使用运行x32代码的正确选项进行编译的. valgrind可能无法用二进制格式混淆 – 它不被认为特别常见. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |