Linux系統共用庫編程
作者:蘇懷廣
一、說明
類似Windows系統中的動態連結程式庫,Linux中也有相應的共用庫用以支援代碼的複用。Windows中為*.dll,而Linux中為*.so。下面詳細介紹如何建立、使用Linux的共用庫。
二、建立共用庫
在mytestso.c檔案中,代碼如下:
#include <stdio.h>
#include <stdlib.h>
int GetMax(int a, int b)
{
if (a >= b)
return a;
return b;
}
int GetInt(char* psztxt)
{
if (0 == psztxt)
return -1;
return atoi(psztxt);
}
然後使用下列命令進行編譯:
gcc -fpic -shared mytestso.c -o mytestso.so
-fpic 使輸出的對象模組是按照可重定位地址方式產生的
編譯成功後,目前的目錄下有mytestso.so,此時已成功建立共用庫mytestso.so。
三、使用共用庫
共用庫中的函數可被主程式載入並執行,但是不必編譯時間連結到主程式的目標檔案中。主程式使用共用庫中的函數時,需要事Crowdsourced Security Testing道所包含的函數的名稱(字串),然後根據其名稱獲得該函數的起始地址(函數指標),然後即可使用該函數指標使用該函數。
在mytest.c檔案中,代碼如下:
#include <dlfcn.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
void* pdlhandle;
char* pszerror;
int (*GetMax)(int a, int b);
int (*GetInt)(char* psztxt);
int a, b;
char* psztxt = "1024";
// open mytestso.so
pdlhandle = dlopen("./mytestso.so", RTLD_LAZY);
pszerror = dlerror();
if (0 != pszerror) {
printf("%s"n", pszerror);
exit(1);
}
// get GetMax func
GetMax = dlsym(pdlhandle, "GetMax");
pszerror = dlerror();
if (0 != pszerror) {
printf("%s"n", pszerror);
exit(1);
}
// get GetInt func
GetInt = dlsym(pdlhandle, "GetInt");
pszerror = dlerror();
if (0 != pszerror) {
printf("%s"n", pszerror);
exit(1);
}
// call fun
a = 200;
b = 600;
printf("max=%d"n", GetMax(a, b));
printf("txt=%d"n", GetInt(psztxt));
// close mytestso.so
dlclose(pdlhandle);
}
然後使用如下命令進行編譯:
gcc mytest.c -ldl -o mytest
-ldl選項,表示產生的對象模組需要使用共用庫
(1)dlopen()
第一個參數:指定共用庫的名稱,將會在下面位置尋找指定的共用庫。
-環境變數LD_LIBRARY_PATH列出的用分號間隔的所有目錄。
-檔案/etc/ld.so.cache中找到的庫的列表,用ldconfig維護。
-目錄usr/lib。
-目錄/lib。
-目前的目錄。
第二個參數:指定如何開啟共用庫。
-RTLD_NOW:將共用庫中的所有函數載入到記憶體
-RTLD_LAZY:會推後共用庫中的函數的載入操作,直到調用dlsym()時方載入某函數
(2)dlsym()
調用dlsym時,利用dlopen()返回的共用庫的phandle以及函數名稱作為參數,返回要載入函數的入口地址。
(3)dlerror()
該函數用於檢查調用共用庫的相關函數出現的錯誤。
四、結束語
本文主要闡述了Linux系統使用共用庫的編程問題,並通過一個簡單的執行個體詳細說明。