When dlopen is alone, the character is not covered by the main character, why?

libp2.c

#include <stdio.h>
void pixman()
{
    printf("pixman in libp1\n");
}

libc2.c

#include <stdio.h>

void pixman();
void cairo()
{
    printf("cairo2\n");
    pixman();
}

main.c

#include <stdio.h>
#include <dlfcn.h>


void pixman()
{
    printf("pixman in main\n");
}

int main()
{
    pixman();

    void* handle=NULL;
    void (*callfun)();

    handle=dlopen("/home/zpeng/test/so_test/libc2.so",RTLD_LAZY);
    callfun = (void(*)())dlsym(handle, "cairo");
    callfun();
...
}

compilation

gcc -c  libp2.c -fPIC   -olibp2.o
rm libp2.a
ar -rs libp2.a libp2.o
gcc -shared -fPIC libc2.c  ./libp2.a -o libc2.so
gcc main.c  -ldl -L.  -g

result:

pixman in main
cairo2
pixman: libp2

why the latter is not "pixman in main"?

I see character processing (LD_DEBUG = characters), it starts with:

21180:     symbol=pixman;  lookup in file=./a.out
21180:     symbol=pixman;  lookup in file=/lib64/libdl.so.2
21180:     symbol=pixman;  lookup in file=/lib64/tls/libc.so.6
21180:     symbol=pixman;  lookup in file=/lib64/ld-linux-x86-64.so.2
21180:     symbol=pixman;  lookup in file=/home/zpeng/test/so_test/libc2.so

if I add -lc2 or -rdynamic to gcc main cmd, it will generate:

pixman in main
cairo2
pixman in main

My questions:

why look for a character in a.out but not get the result and continue searching libc2.so when without -rdynamic and -lc2?

+4
source share
2 answers

"pixman in main"? , GOT. cairo libc2.so, pixman, , , .so .

:

# creates object file only -- contains first pixman implementation
gcc -c  libp2.c -fPIC   -olibp2.o

# just turns the object file into an archive
ar -rs libp2.a libp2.o

# creates the .so file -- all symbols in libc2.c are resolved here 
# and you passed in the .a file for that purpose. The .a file containing the
# first pixman implementation gets put in libc2.so.
gcc -shared -fPIC libc2.c  ./libp2.a -o libc2.so  

, libc2.so, , libc2.so. , , a.out, , . pixman a.out, libc2.so .

a.out, libc2.so, -rdynamic -lc2? rdynamic - , , , (lc2 ). , - pixman. main.c. , , , .

+2

, .a -fvisibility=hidden, , , , , . .a "t" nm -a "T", , .

0

All Articles