簡體   English   中英

為什么我不能直接在Linux中啟動共享庫?

[英]Why can't I directly start a shared library in Linux?

$ chmod +x libsomelibrary.so
$ ./libsomelibrary.so
Segmentation fault

$ gcc -O2 http://vi-server.org/vi/bin/rundll.c -ldl -o rundll
$ ./rundll ./libsomelibrary.so main
(application starts normally)

為什么我不能只啟動libsomelibrary.so,如果它有可用的入口點?

rundll.c是微不足道的:

void* d = dlopen(argv[1], RTLD_LAZY);
void* m = dlsym(d, argv[2]);
return ((int(*)(int,char**,char**))m)(argc-2, argv+2, envp);

為什么在嘗試加載二進制文件時不在內部使用?

main不是內核或動態鏈接器識別的入口點 - 它在編譯時鏈接到可執行文件中的啟動代碼調用(默認情況下,此類啟動代碼未鏈接到共享庫)。

ELF頭包含起始地址。

共享庫不能直接運行。 它們被設計為鏈接到另一個代碼庫。 它可能具有可用的入口點,但是可執行的不僅僅需要具有可用的入口點。 rundll實用程序證明了這一點。 你的第二個測試表明共享庫確實是可執行的,但只有在rundll做了一些工作之后。 如果您對在執行庫代碼之前必須完成的工作感到好奇,請查看rundll的源代碼。

可以在Linux中啟動共享庫。

例如,如果啟動/lib/libc.so.6 ,它將打印出其版本號:

$ /lib/libc.so.6
GNU C Library stable release version 2.12, by Roland McGrath et al.
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.5.0 20100520 (prerelease).
Compiled on a Linux 2.6.34 system on 2010-05-29.
Available extensions:
        crypt add-on version 2.1 by Michael Glad and others
        GNU Libidn by Simon Josefsson
        Native POSIX Threads Library by Ulrich Drepper et al
        BIND-8.2.3-T5B
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.

您的圖書館必定缺少某些內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM