1. 程式人生 > >ld: i386 architecture of input file `write.o' is incompatible with i386:x86-64 output報錯

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

當我在進行一個簡單的編譯一個核心的時候,在輸入如下命令後

 nasm -f elf -o write.o write.S
 ld -m elf_i386 -s -o write.bin write.o

出現報錯如下:

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

原因是我用的ubuntu是64位的,在x86_64上編譯/連結32位應用程式時,設定模擬以elf_i386提供正確的elf格式。

解決方案:將連結的語句換成下面這個

ld -m elf_i386 -s -o write.bin write.o

即可

順便附上我的第一個用匯編寫的系統呼叫:write函式

section .data
str_c_lib: db "c library say : hello world!",0xa
str_c_lib_len equ $-str_c_lib


section .text
global _start


_start:
	
	push str_c_lib_len
	push str_c_lib
	push 1
	call simu_write
	add esp,12

	mov eax,1
	int 0x80

simu_write:
	push ebp
	mov ebp,esp
	mov eax,4
	mov ebx,[ebp+8]
	mov ecx,[ebp+12]
	mov edx,[ebp+16]
	int 0x80
	pop ebp
	ret

執行結果截圖:
在這裡插入圖片描述