#include<stdio.h>
#include<dlfcn.h>
typedef
int
(*fn_B_setName)(
long
pthis,
char
*name);
typedef
void
(*fn_A_A)(
long
pthis,
char
*name,
int
age);
typedef
void
(*fn_A_hello)(
long
pthis,
long
a2);
int
get_baseaddr(
char
* filename, unsigned
long
* baseaddr)
{
unsigned
long
start=0;
unsigned
long
end;
char
line[4096]={0};
char
modulefile[1024]={0};
char
flags[32]={0};
FILE
*fp=NULL;
int
ret=-1;
fp =
fopen
(
"/proc/self/maps"
,
"r"
);
if
(fp == NULL) {
return
ret;
}
while
(
fgets
(line,
sizeof
(line), fp) != NULL) {
sscanf
(line,
"%lx-%lx %s %*Lx %*x:%*x %*Lu %s"
, &start, &end, flags, modulefile);
if
(
strstr
(modulefile, filename)!=NULL) {
*baseaddr=start;
ret = 0;
break
;
}
}
fclose
(fp);
return
ret;
}
int
main()
{
printf
(
"load libdemo.so\n"
);
void
* libdemo_handle = dlopen(
"./libdemo.so"
, RTLD_NOW );
printf
(
"%016lx\n"
, libdemo_handle);
fprintf
(stderr,
"%s\n"
, dlerror());
unsigned
long
baseaddr=0;
get_baseaddr(
"libdemo.so"
, &baseaddr);
printf
(
"baseaddr=0x%016lx\n"
, baseaddr);
fn_B_setName B_setName = (fn_B_setName)(baseaddr+0x12F8);
fn_A_A A_A = (fn_A_A)(baseaddr+0x115A);
fn_A_hello A_hello = (fn_A_hello)(baseaddr+0x11CA);
char
a[1024]={0};
char
b[1024]={0};
B_setName((
long
)b,
"MM"
);
A_A((
long
)a,
"GG"
, 21);
A_hello((
long
)a, (
long
)b);
return
0;
}