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;
}

gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44).

2009/11/26 Richard Henderson <r...@redhat.com>:
> On 11/25/2009 06:24 PM, yunfeng zhang wrote:
>>
>> It seems that original limitation isn't clear or sufficient
>>
>> For a sample:
>>
>> // f.c
>> int g;
>> void foo(void)
>> {
>>        g = 1;
>> }
>>
>> compile with `gcc -shared -fPIC -Wl,-soname,f.so,-Map,f.map -o f.so
>> f.c'...
>
> With -fPIC, the variable G may be overridden by another variable of the same
> name from another shared object earlier in the search path.  That is, the
> offset is *not* fixed because the final address of G may reside in a
> different .so file.
>
> Change your program to
>
>  static int g;
>
> or
>
>  extern int g __attribute__((visibility("hidden")));
>  int g;
>
> and compare the results.  In either case G is known to resolve to the
> instance present in f.so.  In either case we'll use a constant offset.
>
> You really need to understand how ELF actually works before suggesting that
> it's broken.
>
>
> r~
>

Reply via email to