On 11/26/2009 02:04 PM, yunfeng zhang wrote:
The result is the same
#include<stdio.h>
extern int g __attribute__((visibility("hidden")));
int g;
int foo(int a, int b)
{
g = a + b;
printf("%x, %x",&g, foo);
return g;
}
load and call `foo' in the library, an outputting (with vdso) is
cc15bc, cc03fc
and open f.map
0x15bc, 0x3fc
It shows Linux simply maps the library to memory *using* library segment layout.
Using e.cc to call it
#include<exception>
#include<typeinfo>
#include<cstddef>
#include<dlfcn.h>
#include<stdio.h>
int main(void)
{
void* handle = dlopen("./f.so", RTLD_NOW);
typedef int (*gso)(int, int);
gso f;
*(void**) (&f) = dlsym(handle, "foo");
f(1, 2);
return 0;
}
You got the bad test case. Please try the following:
$ cat f.c
#include <stdio.h>
int g;
int foo(int a, int b)
{
g = a + b;
printf("&g = 0x%x, foo = 0x%x\n", &g, foo);
return g;
}
$ cat e.c
int g;
extern int foo(int a, int b);
int main(void)
{
foo(1, 2);
return 0;
}
$ gcc -shared -fPIC -Wl,-soname,./libf.so,-Map,f.map -o libf.so f.c
$ gcc -o e e.c -ldl -L. -lf
$ ./e
&g = 0x600a30, foo = 0x294a2614
Then comment out the "int g;" in e.c. and do the same steps as above:
$ ./e
&g = 0x58294948, foo = 0x58094614
You can see that "C-A" is *not* a constant. Your premise is wrong.
Jie