linux - ELF 可执行问题

标签 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, version 1 (SYSV), statically linked, not stripped
test64-x86_64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

对我来说很有意义。但是,运行它们会带来麻烦。

  • ./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 在运行 ./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 可能被二进制格式搞糊涂了——它并不被认为特别常见。

关于linux - ELF 可执行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34874416/

相关文章:

python - 如何通过添加自定义部分和符号来编辑 ELF

linux - 自动安装 Apache Ant

Linux 读写权限仅通过应用程序

linux - 如何使用sed替换bash中后跟0个或更多空格的命令

android - 什么是 ELF 二进制文件中的 .init_array 部分?

linker - 究竟什么是目标文件中的符号引用?

c - 在静态和动态链接期间隐藏符号,暴露给 dlsym 以进行动态加载

java - Tomcat 不工作但 apache 工作。在 CentOs 6.x 上

linux - 使用官方安装程序安装 Netbeans 12.0 时出错

python 脚本转储 ELF(核心和输出)?