链接、装载和库看完这个系列就够了(六)(-Wl,-export-dynamic参数)

首先我们来看一个简单的程序:

//lib_so1.c 
int fun();
int fun_so()
{
        fun();
        return 0;
}
//main.c 
#include <stdio.h>
int fun_so();

int fun()
{
        printf("in main\n");
        return 0;
}

int main()
{
        fun_so();
        return 0;
}
#Makefile 
all:main

lib_so1.so: lib_so1.o
        gcc -shared -o $@ $< 

main: main.o lib_so1.so
        gcc -o $@ main.o -l_so1 -L. -Wl,--rpath=.

%.o : %.c
        gcc -fPIC -c $< -o $@

clean:
        rm -rf *.o *.a *.so main

程序调用关系很简单,main()->fun_so()->fun(),其中fun_so函数定义在动态库中,其他两个函数定义在main.o中,更多的情况是fun函数通过函数指针的方式传入,但是为了简单,我们是直接使用在动态库调用。

# make 
gcc -fPIC -c main.c -o main.o
gcc -fPIC -c lib_so1.c -o lib_so1.o
gcc -shared -o lib_so1.so lib_so1.o 
gcc -o main main.o -l_so1 -L. -Wl,--rpath=.
# ./main 
in main

运行输出正常。然后我们修改一下程序,把动态库改成运行时调用,也就是使用dlopen方式。
修改Makefile(主要是把动态链接去掉,然后添加-ldl):

#Makefile 
all:main

lib_so1.so: lib_so1.o
        gcc -shared -o $@ $< 

main: main.o lib_so1.so
        gcc -o $@ main.o -ldl

%.o : %.c
        gcc -fPIC -c $< -o $@

clean:
        rm -rf *.o *.a *.so main

修改main.c:

//main.c 
#include <dlfcn.h>
#include <stdio.h>
int fun_so();

int fun()
{
        printf("in main\n");
        return 0;
}

int main()
{
        void *handle;
        int (* p)();
        handle = dlopen ("./lib_so1.so", RTLD_LAZY);
        if (!handle) {
            printf ("dlopen error\n");         
            return -1;
        }

        p = dlsym(handle, "fun_so");     

        (*p)();
        dlclose( handle );
        return 0;
}

lib_so1.c保持不变,然后编译运行:

# make clean;make
rm -rf *.o *.a *.so main
gcc -fPIC -c main.c -o main.o
gcc -fPIC -c lib_so1.c -o lib_so1.o
gcc -shared -o lib_so1.so lib_so1.o 
gcc -o main main.o -ldl
# ./main 
./main: symbol lookup error: ./lib_so1.so: undefined symbol: fun

问题出来了,我们发现运行报错,找不到符号fun。这是什么原因,我们在main.c中定义了fun,为什么会找不到符号?我们使用readelf -sD main查看一下动态符号表,发现什么都没有输出。
原因是动态链接过程中如果发现未定义的动态符号,链接器会把动态符号加入动态符号表(所以第一个例子中,程序输出正常),但是fun符号因为是在dlopen中(dlopen发生在运行过程中)调用的,不会加入到动态符号表,所以导致找不到符号。

-Wl,-export-dynamic

我们在链接过程中加入参数后执行:

# gcc -o main main.o -ldl -Wl,-export-dynamic
# ./main 
in main

我们发现程序输出正常,-Wl,-export-dynamic参数的意义是主程序的所有符号都添加到动态链接符号表中,不过这个符号是否被引用。
我们看一下动态符号表:

# readelf -sD main

Symbol table of `.gnu.hash' for image:
  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name
   10   0: 0000000000201010     0 NOTYPE  GLOBAL DEFAULT  23 _edata
   11   0: 0000000000201000     0 NOTYPE  GLOBAL DEFAULT  23 __data_start
   12   0: 0000000000201018     0 NOTYPE  GLOBAL DEFAULT  24 _end
   13   1: 0000000000201000     0 NOTYPE  WEAK   DEFAULT  23 data_start
   14   1: 0000000000000a30     4 OBJECT  GLOBAL DEFAULT  16 _IO_stdin_used
   15   1: 00000000000009b0   101 FUNC    GLOBAL DEFAULT  14 __libc_csu_init
   16   1: 0000000000000820    43 FUNC    GLOBAL DEFAULT  14 _start
   17   1: 0000000000201010     0 NOTYPE  GLOBAL DEFAULT  24 __bss_start
   18   1: 0000000000000941   108 FUNC    GLOBAL DEFAULT  14 main
   19   1: 00000000000007a8     0 FUNC    GLOBAL DEFAULT  11 _init
   20   2: 0000000000000a20     2 FUNC    GLOBAL DEFAULT  14 __libc_csu_fini
   21   2: 0000000000000a24     0 FUNC    GLOBAL DEFAULT  15 _fini
   22   2: 000000000000092a    23 FUNC    GLOBAL DEFAULT  14 fun

在最后一行我们找到了fun符号。

发布了8 篇原创文章 · 获赞 0 · 访问量 227

猜你喜欢

转载自blog.csdn.net/zhensansan/article/details/104590688
今日推荐