我在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
Run Code Online (Sandbox Code Playgroud)
我有以下程序(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
Run Code Online (Sandbox Code Playgroud)
我创建三个不同的可执行文件:
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
Run Code Online (Sandbox Code Playgroud)
这是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, version 1 (SYSV), statically linked, not stripped
test64-x86_64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
Run Code Online (Sandbox Code Playgroud)
我感觉合理.但是,运行它们会带来麻烦.
./test32-i386:没问题,运行正常../test64-x86_64:同样,运行正常../test32-x86_64 但是,给出 bash: ./test32-x86_64: cannot execute binary file: Exec format error此外,Valgrind产生......有趣的结果.
valgrind ./test32-i386: 好valgrind ./test64-x86_64:加注SIGILL(?!)valgrind ./test32-x86_64: 给我 ./test32-x86_64: 1: ./test32-x86_64: Syntax error: word unexpected (expecting ")")问题1:为什么Valgrind SIGILL在运行时加注,./test64-x86_64即使程序在没有Valgrind的情况下似乎工作正常?
问题2:为什么我不能跑./test32-x86_64?Valgrind为该二进制文件提供的错误非常模糊......
对于问题 1:有一个针对 valgrind 的错误,它不支持x86_64 中的 int80 指令。我能够在我自己的 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
Run Code Online (Sandbox Code Playgroud)
即类是32位,机器类型是x86_64。即它是一个 x32 ABI 二进制文件
问题是,这需要您的内核进行配置CONFIG_X86_X32_ABI,否则您将违反检查:
#define compat_elf_check_arch(x) \
(elf_check_arch_ia32(x) || \
(IS_ENABLED(CONFIG_X86_X32_ABI) && (x)->e_machine == EM_X86_64))
Run Code Online (Sandbox Code Playgroud)
仅支持 32 位二进制文件,不带配置选项。如果您有以下内核选项,则设置此选项: CONFIG_X86_X32=y 和 CONFIG_X86_X32_DISABLED 未设置(这是我正在查看的 Linux 内核 4.3 源代码)。
因此,您需要配置具有此支持的内核才能运行代码 - perror 没有看到该问题的原因是他的内核似乎是使用运行 x32 代码的正确选项进行编译的。
valgrind 可能对二进制格式感到困惑——它并不被认为是特别常见的。
| 归档时间: |
|
| 查看次数: |
1259 次 |
| 最近记录: |