科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道Linux操作系统动态函式库探究(一) (2)

Linux操作系统动态函式库探究(一) (2)

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

在 Linux 中,执行档我们可以编程成静态联结以及动态连结,以下我们举一个简短的程序作为例子:

作者:www.linuxbyte.net 来源:www.linuxbyte.net 2007年10月20日

关键字: 操作系统 动态 Linux

  • 评论
  • 分享微博
  • 分享邮件

我们可以注意到 e_type: ET_EXEC,这就是 ELF 档中对於执行档所定义的档案属性。

动态连结 VS 静态联结

在 Linux 中,执行档我们可以编程成静态联结以及动态连结,以下我们举一个简短的程序作为例子:

#include 
int main() 
{ 
printf(\"\\ntest\"); 
}

若我们执行 :

[root@hlchou /root]# gcc test.c -o test

所产生出来的执行档 test,预设为使用动态函式库,所以我们可以用以下的指令 :

[root@hlchou /root]# ldd test 
libc.so.6 => /lib/libc.so.6 (0x40016000) 
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

来得知目前该执行档共用了哪些动态函式库,以我们所举的 test 执行档来说,共用了两个动态函式库,分别为 libc.so.6 与 ld-linux.so.2。我们还可以透过下面的 file 指令,来得知该执行档的相关属性,如下:

[root@hlchou /root]# file test 
test: ELF 32-bit LSB executable, Intel 80386, 
version 1, dynamically linked (use 
s shared libs), not stripped

not stripped 表示这个执行档还没有透过 strip 指令来把执行时用不到的符号、以及相关除错的资讯删除,举个例子来说,目前这个test 执行档大小约为 11694 bytes:

[root@hlchou /root]# ls -l test 
-rwxr-xr-x 1 root root 11694 Oct 24 02:31 test

经过strip後,则变为 3004 bytes:

[root@hlchou /root]# strip test 
[root@hlchou /root]# ls -l test 
-rwxr-xr-x 1 root root 3004 Oct 24 02:48 test

不过读者必须注意到一点,经过 strip 过的执行档,就无法透过其它的除错软件从里面取得函式在编程时所附的相关资讯,这些资讯对我们在除错软件时,可以提供不少的帮助,各位在应用上请自行注意。

相对於编程出来使用动态函式库的执行档 test,我们也可以做出静态联结的执行档 test:

[root@hlchou /root]# gcc -static test.c -o test

透过指令 ldd,我们可以确定执行档 test 并没有使用到动态函式库:

[root@hlchou /root]# ldd test 
not a dynamic executable

再透过指令 file,可以注意到 test 目前为 statically linked,且亦尚未经过 strip:

[root@hlchou /root]# file test 
test: ELF 32-bit LSB executable, Intel 80386, 
version 1, statically linked, not stripped

相信大夥都会好奇,使用静态联结,且又没有经过 strip 删去不必要的符号的执行档的大小会是多少,透过 ls -l来看,我们发现大小变成 932358 bytes 比起静态联结的执行档大了相当多:

[root@hlchou /root]# ls -l test 
-rwxr-xr-x 1 root root 932258 Oct 24 02:51 test

若再经过 strip,则档案大小变为 215364 bytes:

[root@hlchou /root]# strip test 
[root@hlchou /root]# ls -l test 
-rwxr-xr-x 1 root root 215364 Oct 24 02:55 test
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章