linux 运行unix elf,在Linux中ELF可执行问题
我在
Linux上有一些關(guān)于ELF可執(zhí)行文件的奇怪問題.
這是我的系統(tǒng)(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
我創(chuàng)建三個(gè)不同的可執(zhí)行文件:
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, version 1 (SYSV), statically linked, not stripped
test64-x86_64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
我感覺合理.但是,運(yùn)行它們會(huì)帶來麻煩.
> ./test32-i386:沒問題,運(yùn)行正常.
> ./test64-x86_64:同樣,運(yùn)行正常.
> ./test32-x86_64但是,給出了bash:./ test32-x86_64:無法執(zhí)行二進(jìn)制文件:exec格式錯(cuò)誤
此外,Valgrind產(chǎn)生……有趣的結(jié)果.
> valgrind ./test32-i386:好的
> valgrind ./test64-x86_64:提出SIGILL(?!)
> valgrind ./test32-x86_64:給我./test32-x86_64:1:./ test32-x86_64:語法錯(cuò)誤:單詞意外(期待“)”)
所以,總結(jié)一下:
問題1:為什么Valgrind在運(yùn)行./test64-x86_64時(shí)會(huì)引發(fā)SIGILL,即使程序在沒有Valgrind的情況下似乎工作正常?
問題2:為什么我不能運(yùn)行./test32-x86_64? Valgrind為該二進(jìn)制文件提供的錯(cuò)誤非常模糊……
對于問題1:有一個(gè)針對valgrind的錯(cuò)誤,它不支持
int80 instruction in x86_64.我能夠在我自己的valgrind(v3.11.0)下重現(xiàn)這一點(diǎn),并且從瀏覽源看起來好像它不受支持.
對于問題2:ELF加載程序不支持文件類型.為了在Linux上提供32位二進(jìn)制文??件的兼容性,它必須在嘗試執(zhí)行二進(jìn)制文件時(shí)對二進(jìn)制文件進(jìn)行一些檢查.
當(dāng)我們在test32-x86_64上使用readelf時(shí),它會(huì)顯示一個(gè)標(biāo)題:
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位,機(jī)器類型為x86_64.即它是x32 ABI二進(jìn)制文件
問題是這需要你的內(nèi)核配置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))
它只支持沒有配置選項(xiàng)的32位二進(jìn)制文??件.如果您有內(nèi)核選項(xiàng),則設(shè)置此選項(xiàng):CONFIG_X86_X32 = y和CONFIG_X86_X32_DISABLED未設(shè)置(這是我正在查看的Linux內(nèi)核4.3源代碼).
因此,您需要使用此支持配置的內(nèi)核來運(yùn)行代碼 – perror之所以沒有看到問題是他的內(nèi)核似乎是使用運(yùn)行x32代碼的正確選項(xiàng)進(jìn)行編譯的.
valgrind可能無法用二進(jìn)制格式混淆 – 它不被認(rèn)為特別常見.
總結(jié)
以上是生活随笔為你收集整理的linux 运行unix elf,在Linux中ELF可执行问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: indesign照片放入太大_猪肚鸡做法
- 下一篇: mysql架设_主从mysql架设