Intel汇编 ld链接错误

ld: i386 architecture of input file `xxxx.o’ is incompatible with i386:x86-64 output

最近在ubuntu20.04系统上编译一段简单的shellcode汇编代码时遇到了标题中的错误,输入命令如下:

nasm -f elf -o shellcode.o shellcode.asm
ld -o shellcode shellcode.o

出现错误如下:

ld: i386 architecture of input file `shellcode.o' is incompatible with i386:x86-64 output

原因是我使用的ubuntu20.04是64位系统,而我的code是32位应用程序。在x86_64上编译/链接32位应用程序时,需要设置模拟elf_i386来提供正确的elf格式。
解决方案:

ld -m elf_i386 -s -o shellcode shellcode.o

执行的shellcode.asm代码如下:

section .txt
global _start
_start:
    xor eax, eax
    push eax    ;"\x00"
    push 0x68732f2f    ;"//sh"
    push 0x6e69622f    ; /"bin"
    mov ebx, esp
    push eax    ; "\x00" to stack
    push ebx    ; store "/bin//sh" to stack
    mov ecx, esp    ; ecx = esp
    xor edx, edx    ; edx = 0
    mov al, 0xb    ; al = 11 the call number of execve
    int 0x80
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!