代码之家  ›  专栏  ›  技术社区  ›  Vi.

为什么我不能在Linux中直接启动共享库?

  •  3
  • Vi.  · 技术社区  · 15 年前
    $ 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)
    

    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);
    

    为什么在尝试加载二进制文件时不在内部使用?

    3 回复  |  直到 15 年前
        1
  •  4
  •   caf    15 年前

    main 不是内核或动态链接器识别的入口点-它是在编译可执行文件时由链接到可执行文件的启动代码调用的(默认情况下,此类启动代码未链接到共享库)。

    ELF头包含起始地址。

        2
  •  3
  •   bta    15 年前

    rundll 效用就是证明。第二个测试表明共享库确实是可执行的,但是只有在 运行dll 运行dll .

        3
  •  3
  •   Jörg W Mittag    15 年前

    可以 在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>.