標頭檔 #include <dlfcn.h>
載入動態庫
void *dlopen(const char *filename, int flag);
flag的可能值:
l RTLD_LAZY
l RTLD_NOW
l RTLD_GLOBAL
l RTLD_LOCAL
l RTLD_NODELETE (since glibc 2.2)
l RTLD_NOLOAD (since glibc 2.2)
l RTLD_DEEPBIND
這些flag的具體含義可使用man查看
返回動態庫中最近的一次錯誤
char *dlerror(void);
根據動態庫控制代碼和函數名稱,返回函數的地址
void *dlsym(void *handle, const char *symbol);
如果使用dlopen(NULL, <flag>)得到控制代碼,通過這個控制代碼可以尋找所有函數符號,只要那個函數符號所在的dll在載入時指定了RTLD_GLOBAL
關閉動態庫
int dlclose(void *handle);
根據函數地址,返回函數名稱和其它資訊
注意傳入地址只要落在函數代碼空間範圍之內即可,不需要是函數的起始地址
int dladdr(void *addr, Dl_info *info);
typedef struct {
const char *dli_fname;/* Filename of defining object */
void *dli_fbase; /* Load address of that object */
const char *dli_sname;/* Name of nearest lower symbol */
void *dli_saddr; /* Exact value of nearest symbol */
} Dl_info;
建構函式和解構函式(這兩個是屬性,而不是函數)
__attribute__((constructor))
__attribute__((destructor))
例如:
__attribute__((constructor)) void before_main()
{
printf("before main/n");
}
__attribute__((destructor)) void after_main()
{
printf("after main/n");
}