如何创建一个可执行的 Linux 共享库

1 前言

前段时间,有多位同学在“泰晓原创团队”微信群聊到 C 语言相关的两个问题:

  • 如何让共享库文件也可以直接执行
  • 如何在可执行文件中用 dlopen 解析自身的函数

这两个需求汇总起来,可以大体理解为如何让一个程序既可以作为共享库,又能够直接运行。

这类需求在 Linux 下面其实很常见,比如 ld-linux.so 和 libc.so:

 
  1. $ file /lib/i386-linux-gnu/ld-linux.so.2
  2. /lib/i386-linux-gnu/ld-linux.so.2: symbolic link to ld-2.23.so
  3. $ file /lib/i386-linux-gnu/ld-2.23.so
  4. /lib/i386-linux-gnu/ld-2.23.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked
  5. $ /lib/i386-linux-gnu/ld-2.23.so
  6. Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]
  7. $ file /lib/i386-linux-gnu/libc.so.6
  8. /lib/i386-linux-gnu/libc.so.6: symbolic link to libc-2.23.so
  9. $ file /lib/i386-linux-gnu/libc-2.23.so
  10. /lib/i386-linux-gnu/libc-2.23.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (GNU/Linux), dynamically linked
  11. $ /lib/i386-linux-gnu/libc.so.6
  12. GNU C Library (Ubuntu GLIBC 2.23-0ubuntu11) stable release version 2.23, by Roland McGrath et al.

那如何做到的呢?

2 先来看看两类文件的区别

当前 Linux 下面的二进制程序标准格式是 ELF,这类格式可以用来表示 4 种不同类型的文件:

  • 可重定位目标文件(.o),用于静态链接
  • 可执行文件格式,用于运行时创建进程映像
  • 共享目标文件(.so,共享库),协同可执行文件创建进程映像
  • Core dump(core),运行过程中崩溃时自动生成,用于调试

我们来看中间两类:

  • 可执行文件
    • 如果不引用外部库函数,那么所有符号地址是确定的,执行加载后可直接运行
  • 共享库
    • 如果可执行文件用到外部库函数,那么需要通过动态链接器加载引用到的共享库并在运行时解析用到的相应符号

所以,前者和后者通常情况下不是独立存在的,是联合行动的,两者差异明显:

  • 可执行文件有标准的 C 语言程序执行入口 main,而共享库则并没有这类强制要求
  • 后者为了确保可以灵活被多个可执行文件共享,所以,符号地址在链接时是相对的,在装载时动态分配和计算符号地址

接下来做个实验具体看看两者的区别,准备一个“烂大街”的 hello.c 先:

 
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. printf("hello\n");
  5. return 0;
  6. }

先来编译为可执行文件(-m32 用来生成采用 i386 指令集的代码):

 
  1. $ gcc -m32 -o hello hello.c
  2. $ file hello
  3. hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-
  4. $ ./hello
  5. hello

再来编译为共享目标文件,并尝试直接执行它:

 
  1. $ gcc -m32 -shared -fpic -o libhello.so hello.c
  2. $ file libhello.so
  3. libhello.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked
  4. $ ./libhello.so
  5. Segmentation fault (core dumped)

直接执行失败,再试试如何生成一个可执行文件来加载运行它,这个是引用共享库的通常做法:

 
  1. $ gcc -m32 -o hello.noc -L./ -lhello
  2. $ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./ ./hello.noc
  3. hello

通过实验,可以确认“正常”创建出来的共享库并不能够直接运行,而是需要链接到其他可执行文件中。

上述编译选项简介:

-shared Create a shared library.

-fpic Generate position-independent code (PIC) suitable for use in a shared library

3 让可执行文件可共享

接下来,好好研究一番。

先来看一个 gcc 直接支持的方式:

 
  1. $ gcc -m32 -pie -fpie -rdynamic -o libhello.so hello.c
  2. $ file libhello.so
  3. libhello.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked
  4. $ ./libhello.so
  5. hello
  6. $ gcc -m32 -o hello.noc -L./ -lhello
  7. $ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./ ./hello.noc
  8. hello

确实可以执行,而且可以作为共享库链接到其他可执行文件中。

上述编译选项简介:

-pie Produce a position independent executable on targets that support it.

-fpie These options are similar to -fpic and -fPIC, but generated position independent code can be only linked into executables.

-rdynamic Pass the flag -export-dynamic to the ELF linker, on targets that support it. This instructs the linker to add all symbols, not only usedones, to the dynamic symbol table.

-rdynamic 等价于 -Wl,-E / -Wl,--export-dynamic,确保所有“库”中的符号都 export 到动态符号表,包括当前未用到的那些符号。

举个例子,如果 hello.c 有一个独立的 hello() 函数,没有别的函数(这里是指 main)调用到,但是其他用到该库的可执行文件希望用到它,那么 -rdynamic 就是必须的。

 
  1. $ cat hello.c
  2. #include <stdio.h>
  3. void hello(void)
  4. {
  5. printf("hello...\n");
  6. }
  7. int main(void)
  8. {
  9. printf("hello\n");
  10. return 0;
  11. }
  12. $ cat main.c
  13. #include <stdio.h>
  14. extern void hello(void);
  15. int main(void)
  16. {
  17. hello();
  18. return 0;
  19. }
  20. $ gcc -m32 -pie -fpie -rdynamic -o libhello.so hello.c
  21. $ readelf --dyn-syms libhello.so | grep hello
  22. 19: 00000662 43 FUNC GLOBAL DEFAULT 14 hello
  23. $ gcc -m32 -o hello.main main.c -L./ -lhello
  24. $ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./ ./hello.main
  25. hello...

如果没有 -rdynamic,链接时就没法使用。

 
  1. $ gcc -m32 -o hello.main main.c -L./ -lhello
  2. main.c:(.text+0x7): undefined reference to `hello'

同理,dlopen 自解析时也需要 -rdynamic

 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define _GNU_SOURCE
  4. #include <dlfcn.h>
  5. void hello(void)
  6. {
  7. printf("hello...\n");
  8. }
  9. int main(void)
  10. {
  11. void *handle;
  12. void (*func)(void);
  13. char *error;
  14. handle = dlopen(NULL, RTLD_LAZY);
  15. if (!handle) {
  16. fprintf(stderr, "%s\n", dlerror());
  17. return EXIT_FAILURE;
  18. }
  19. dlerror(); /* Clear any existing error */
  20. func = (void (*)(void)) dlsym(handle, "hello");
  21. error = dlerror();
  22. if (error != NULL) {
  23. fprintf(stderr, "%s\n", error);
  24. return EXIT_FAILURE;
  25. }
  26. func();
  27. dlclose(handle);
  28. return 0;
  29. }

实测效果:

 
  1. $ gcc -m32 -pie -fpie -o libhello.so hello.c -ldl
  2. $ ./libhello.so
  3. ./libhello.so: undefined symbol: hello
  4. $ gcc -m32 -pie -fpie -rdynamic -o libhello.so hello.c -ldl
  5. $ ./libhello.so
  6. hello...

4 让共享库可执行

下面来探讨另外一种方式,在生成共享库的基础上,来研究怎么让它可以执行。

先来回顾一下共享库,在本文第 2 节直接执行的时候马上出段错误,基本原因是共享库没有强制提供一个标准的 C 程序入口。

即使是我们提供了 main()(把标准 hello.c 编译为 libhello.so),程序的入口并没有指向它。

 
  1. $ readelf -h libhello.so | grep "Entry point"
  2. Entry point address: 0x3d0
  3. $ objdump -d libhello.so | grep 3d0 | head -2
  4. 380: e8 4b 00 00 00 call 3d0 <__x86.get_pc_thunk.bx>
  5. 000003d0 <__x86.get_pc_thunk.bx>:

那么,先解决入口的问题并运行,同样出错了:

 
  1. $ gcc -m32 -shared -fpic -o libhello.so hello.c -Wl,-emain
  2. $ readelf -h libhello.so | grep "Entry point"
  3. Entry point address: 0x4b9
  4. $ objdump -d libhello.so | grep 4b9 | head -2
  5. 000004b9 <main>:
  6. 4b9: 8d 4c 24 04 lea 0x4(%esp),%ecx
  7. $ ./libhello.so
  8. Segmentation fault

加上 -g 编译用 gdb 来看看原因:

 
  1. $ gcc -m32 -g -shared -fpic -o libhello.so hello.c -Wl,-emain
  2. $ objdump -d libhello.so | grep 4b9 | head -2
  3. 000004b9 <main>:
  4. 4b9: 8d 4c 24 04 lea 0x4(%esp),%ecx
  5. $ ulimit -c unlimited
  6. $ ./libhello.so
  7. Segmentation fault (core dumped)
  8. $ gdb ./libhello.so core
  9. Core was generated by `./libhello.so'.
  10. Program terminated with signal SIGSEGV, Segmentation fault.
  11. #0 0x000003a6 in ?? ()
  12. (gdb) bt
  13. #0 0x000003a6 in ?? ()
  14. #1 0xf77344e3 in main () at hello.c:5
  15. (gdb) l hello.c:5
  16. 1 #include <stdio.h>
  17. 2
  18. 3 int main(void)
  19. 4 {
  20. 5 printf("hello\n");
  21. 6
  22. 7 return 0;
  23. 8 }
  24. (gdb)

可以看到是执行 printf 的时候出错,说明库函数的解析出了问题,主动用动态连接器跑一下看看:

 
  1. $ /lib/i386-linux-gnu/ld-2.23.so ./libhello.so
  2. hello
  3. Segmentation fault (core dumped)

哇哦,可以解析符号并打印了,不过最后还是崩溃了?

如果去分析 glibc 的 __libc_start_main 不难发现,我们还少调用一个标准退出函数,改造过后:

 
  1. $ cat hello.c
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. void main(void)
  5. {
  6. printf("hello\n");
  7. _exit(0);
  8. }

再编译运行就没段错误了。再进一步,同样是分析 glibc,发现实际的入口函数并非 main(),而是 _start

 
  1. $ cat hello.c
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. int main(void)
  5. {
  6. printf("hello\n");
  7. return 0;
  8. }
  9. void _start(void)
  10. {
  11. int ret;
  12. ret = main();
  13. _exit(ret);
  14. }

编译时连入口都不用指定了:

 
  1. $ gcc -m32 -g -shared -fpic -o libhello.so hello.c
  2. $ /lib/i386-linux-gnu/ld-2.23.so ./libhello.so
  3. hello

也可以当共享库使用:

 
  1. $ gcc -m32 -o hello.noc -L./ -lhello
  2. $ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./ ./hello.noc
  3. hello

稍微补充,完整的 _start 还需要处理参数、实现 init/fini 逻辑,这里限于篇幅不做展开。

最后还有一点遗憾,怎么样才能“动态”链接,而不是手动指定动态链接器呢?我们在程序中主动加入一个 .interp 节区来指定动态链接器吧。

 
  1. $ cat hello.c
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. asm(".pushsection .interp,\"a\"\n"
  5. " .string \"/lib/i386-linux-gnu/ld-linux.so.2\"\n"
  6. ".popsection");
  7. int main(void)
  8. {
  9. printf("hello\n");
  10. return 0;
  11. }
  12. void _start(void)
  13. {
  14. int ret;
  15. ret = main();
  16. _exit(ret);
  17. }

再试试,完美运行:

 
  1. $ gcc -m32 -shared -fpic -o libhello.so hello.c
  2. $ ./libhello.so
  3. hello

最后,稍微整理一下:

 
  1. $ cat hello.c
  2. #include <stdio.h>
  3. #ifdef EXEC_SHARED
  4. #include <unistd.h>
  5. asm(".pushsection .interp,\"a\"\n"
  6. " .string \"/lib/i386-linux-gnu/ld-linux.so.2\"\n"
  7. ".popsection");
  8. int entry(void)
  9. {
  10. printf("%s %d: %s(): the real entry of shared library here.\n", __FILE__, __LINE__, __func__);
  11. /* do whatever */
  12. return 0;
  13. }
  14. int main(void)
  15. {
  16. return entry();
  17. return 0;
  18. }
  19. void _start(void)
  20. {
  21. int ret;
  22. ret = main();
  23. _exit(ret);
  24. }
  25. #endif
  26. void hello(void)
  27. {
  28. printf("hello...\n");
  29. }

当普通共享库使用,默认编译即可,要能够执行的话,实现一下 entry(),编译时打开 EXEC_SHARED 即可:

 
  1. $ gcc -m32 -shared -fpic -o libhello.so hello.c -DEXEC_SHARED
  2. $ ./libhello.so
  3. hello.c 12: entry(): the real entry of shared library here.
  4. $ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./ ./hello.main
  5. hello...

5 小结

本文详细讲解了如何像 libc.so 和 ld-linux.so 一样,既可以当共享库使用,还能直接执行,并且讲述了两种方法。

两种方法都可以达成目标,第一种方法用起来简单方便,第二种方法揭示了很多背后的工作逻辑。

如果想与本文作者更深入地探讨共享库、程序执行、动态链接、内联汇编、程序真正入口等本文涉及的内容以及它们背后的程序链接、装载和运行原理,欢迎订阅吴老师的 10 小时 C 语言进阶视频课:《360° 剖析 Linux ELF》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值