在Nasm引入C函式庫
我們一般只引用C語言的函式庫,因為其不包含多型(polymorphism) 而且較原始
比如C++一個函數的名字實際上是經過包裝的 call 0x140001870 <_Z4showv> 不易使用
nasm可以使用extern 函數名來引用外部函數,在link時必須包含該庫
所以這時候用gcc來做link會很方便不用一個個找對應檔案
來看一個簡單範例:
global mainextern puts
section .datamsg: db "Hello", 0
section .textmain: mov rdi, msg call puts wrt ..plt xor rax, rax retextern puts聲明有一個外部的函數puts- puts()函數是int puts( const char* str ); 有一個參數rdi指向字串和4 bytes返回值在eax
- call的時候在puts後面加上
wrt ..plt指向PLT表的入口,因為動態鏈結在組譯階段不知道libc.so中實際函數地址 - gcc要求程式進入點名稱用main包裝,就像是int main() { return 0; }一樣的概念
用nasm組譯並使用gcc鏈結
nasm -f elf64 file.asm -o file.ogcc file.o -o file./file