感覺對自己有用 給點個贊哈
代碼如下
mytest.c
#include<stdio.h>
#include<stdlib.h>
//在elf格式中加入一個節.interp 此節含有 ELF 程序解析器的路徑名
//如果動態庫不包括此節就不能被執行
//注意你的連接器地址不一定是/lib64/ld-linux-x86-64.so.2這個路徑哈 自己查找下你環境中的連接器地址
const char ldpath[] __attribute__ ((section (".interp"))) = "/lib64/ld-linux-x86-64.so.2";
//動態庫執行的入口函數 由編譯器指定(-Wl,-e,myso_entry)
static void myso_entry()
{
printf("this is a output infomation for test\n");
exit(0);
}
/*
下面是動態庫提供的兩個對外函數
*/
int myadd(int a,int b)
{
return a+b;
}
int mysub(int a,int b)
{
return a-b;
}
編譯方式
gcc -fPIC -shared -Wl,-e,myso_entry -o mytest.so mytest.c
Wl:告訴編譯器將後面的參數(-e myso_entry)交給連接器處理
ld連接器使用-e 來指定程序的入口地址(顯然此處指定入口地址爲myso_entry)