[英]Can't link object file using ld - Mac OS X
/*********
exit.asm
*/
[SECTION .text]
global _start
_start:
xor eax, eax
xor ebx, ebx
mov al, 1
int 0x80
//****************************
首先,我使用nasm -f elf exit.asm生成目標文件。
然后我在我的Mac OS X 10.7上運行了以下“ld”命令,它有這些輸出和警告,我試圖在我的32位linux機器上運行它,一切都很順利,請你解釋為什么不會鏈接器在我的Mac上運行?
謝謝!
Alfred says: ld -o exiter exit.o
ld: warning: -arch not specified
ld: warning: -macosx_version_min not specified, assuming 10.7
ld: warning: ignoring file exit.o, file was built for unsupported file format ( 0x7f 0x45 0x4c 0x46 0x 1 0x 1 0x 1 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 ) which is not the architecture being linked (x86_64): exit.o
Undefined symbols for architecture x86_64:
"start", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for inferred architecture x86_64
在我指定我的拱門和版本之后,我得到了:
Alfred says: ld -arch x86_64 -macosx_version_min 10.7 -o exiter exit.o
ld: warning: ignoring file exit.o, file was built for unsupported file format ( 0x7f 0x45 0x4c 0x46 0x 1 0x 1 0x 1 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 ) which is not the architecture being linked (x86_64): exit.o
Undefined symbols for architecture x86_64:
"start", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
獲取程序鏈接是很容易的部分:
_start
start
$ nasm -f macho exit.asm
$ ld -arch i386 -o exiter exit.o
問題是, exit.asm
呼吁在i386的Linux exit()
系統調用(EAX = 1),程序會按預期在OS X與零個狀態不會退出
系統調用是對內核的請求。 與sqrt()
不同, exit()
必須向其實現中具有更高權限的軟件組件發出請求,因為它終止正在運行的程序。 應用無法自行創建或終止進程。 系統調用為應用程序提供了一種讓內核代表他們執行操作的方法。
制作系統調用是這樣的:
1
是exit
的系統調用號。 0
( EBX由xor
清除)是系統調用的第一個參數,即退出狀態。 int 80
sycall
svc
Linux和OS X都為C程序提供了void exit(int)
函數,但不同意如何向內核描述此請求的細節。 exit.asm
的代碼與libc
_exit()
函數的實現處於同一級別。
即使在運行Linux的不同體系結構之間,系統調用號和調用約定也不同。 例如,在x86-64 Linux上, exit(0)
更常見,如下所示:
xor rdi, rdi
mov al, 60
syscall
你可以通過在/lib64/libc.so.6
反匯編_exit
來看到這/lib64/libc.so.6
。
您可以。 但是你必須將程序與libc
鏈接起來。 鏈接exit.asm
與上面的區別是:
$ cc -m32 -nostdlib exit.o -o exiter
和
出口libc.asm
extern exit
global main
main:
push 0
call exit
必須與以下內容聯系:
$ cc -m32 exit-libc.o -o exit-libc
試試這個,看一下文件大小。
Mac OS X不使用ELF,因此您需要生成一個Mach-O對象以在該系統上進行鏈接。 在我的機器上, nasm
似乎只支持32位輸出,因此在鏈接時你也需要匹配該架構。
我還必須更改_start
以start
鏈接。
以下是您的代碼的工作示例:
$ cat exit.asm
[SECTION .text]
global start
start:
xor eax, eax
xor ebx, ebx
mov al, 1
int 0x80
$ nasm -f macho exit.asm
$ ld -arch i386 -macosx_version_min 10.7 -o exiter exit.o
$ ./exiter
$ echo $?
236
請注意,該程序可能無法在Mac OS X上執行您想要的操作,因為它不像Linux那樣執行系統調用。
大多數情況下,當您收到此錯誤時:
ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not
allowed in code signed PIE, but used in _start from hello.o. To fix this
warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
這是因為它正在尋找你的“main()”函數(標簽)給我標簽“start:”。 最好用“ld -e”指定主標簽。
-o hello.tmp - outfile
-f macho - specify format
Linux - elf or elf64
Mac OSX - macho
-arch i386 - specify architecture (32 bit assembly)
-macosx_version_min 10.6 (Mac OSX - complains about default specification)
-no_pie (Mac OSX - removes ld warning)
-e main - specify main symbol name (Mac OSX - default is start)
-o hello.o - outfile
./hello.o - execution
nasm -o hello.tmp -f macho hello.s && ld -arch i386 -macosx_version_min 10.6 -no_pie -e _main -o hello.o hello.tmp && ./hello.o
如果這有幫助,請告訴我!
我在博客上寫了如何做到這一點:
http://blog.burrowsapps.com/2013/07/how-to-compile-helloworld-in-intel-x86.html
為了更詳細的解釋,我在我的Github上解釋說:
標准mac gcc不會鏈接elf對象。 對於需要堅持使用elf格式並在mac上開發的人,你需要一個交叉編譯器......
http://crossgcc.rts-software.org/doku.php?id=compiling_for_linux
然后你可以繼續進行類似的事情......
/usr/local/gcc-4.8.1-for-linux32/bin/i586-pc-linux-ld -m elf_i386 -T link.ld -o kernel kasm.o kc.o
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.