【发布时间】:2018-11-02 11:38:08
【问题描述】:
所以我是新尝试 asm,我想编译一个小程序,它只使用 i386 指令而不是 x86-64 指令退出。我有一个 x86-64 Ubuntu,它可以完美地编译和运行 x86-64,但我不知道如何在同一台 x86-64 机器上组装和链接 i386 版本(我已经安装了 i386 compat)。
我知道已经回答了类似的问题,但是他们都没有使用 as 和 ld 来解决问题,所以我不知道如何将这些解决方案转化为我的问题。
对于 x86-64,我使用 as 和 ld,如下所示:
# Assemble: as exit.s -o exit.o
# Linking: ld exit.o -o exit
x86-32版本的程序:
.section .data
.section .text
.globl _start
_start:
movl $1, %eax
movl $0, %ebx
int $0x80
现在.. 我一直在寻找如何做到这一点,并在 as 和 ld 中找到了 --32 和 ld 中的 -m {arg},但每次我编译它时不会出现拱形错误,它会给我“文件格式错误”错误。
我试过像这样用 elf_i386 和 i386linux 做 ld:
as --32 exit.s -o exit.o
ld -m elf_i386 exit.o -o exit
#Error: -bash: ./exit: cannot execute binary file: File in wrong format
ld -m i386linux exit.o -o exit
#Error: -bash: ./exit: cannot execute binary file: File in wrong format
为了兼容性,我想补充一点,我已经安装了 Ubuntu 帮助论坛中列出的这些软件包:
sudo dpkg --add-architecture i386
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
sudo apt-get install multiarch-support
【问题讨论】:
-
发布您使用的确切命令以及错误。
as --32和ld -melf_i386应该可以工作。 -
嗨@Jester 尝试并得到了上面发布的错误。
-
看起来您的内核是在没有
CONFIG_COMPAT_BINFMT_ELFList of executable formats on Linux 的情况下构建的,因此它无法将 32 位 ELF 静态可执行文件识别为可执行文件。 (顺便说一句,x32 非常不同:它是 64 位模式下的 32 位指针en.wikipedia.org/wiki/X32_ABI。此外,x64 是仅限 Windows 的术语,Linux 使用 x86-64)。 -
好的,谢谢指出。
标签: linux assembly x86 linker multiarch