dlopen() - 这个未定义的符号错误是否与依赖项有关?

问题描述

问题的背景在一个已经回答的问题中,here

我有一个想要在共享库中使用的符号,但尽管通过 T 被列为 nm 定义的符号,F函数)通过​​ objdump ,当我尝试使用 dlopen() 或通过 Python (ctypes.CDLL) 访问它时,它似乎不存在或未定义。

我读到 here 这可能与依赖项有关。

ldd输出

linux@host:~$ ldd ./compiled_program
linux-vdso.so.1 (0x00007ffde1fdf000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3381e7c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f338246f000)

dn -DC输出

linux@host:~$ nm -DC ./compiled_program
             w _ITM_deregisterTMCloneTable
             w _ITM_registerTMCloneTable
             w __cxa_finalize
             w __gmon_start__
             U __libc_start_main
             U __stack_chk_fail
             U printf
             U putchar
             U puts
             U rand
             U srand
             U time

由于我对 C 的了解有限,我无法判断其中是否有任何可能是未解决的依赖项,以及在哪里查找要加载的库(如答案所示)以包含它们。是查找库并添加 #include <...> 还是我必须找到库并使用 dlopen() 加载它一样简单?答案似乎暗示了后者。我很感激我在这里完全在黑暗中挣扎!

我当前的 dlopen 代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

int main(int argc,char** argv) {
    void *handle;
    void (*func_in_question)(const char*);
    handle = dlopen("./compiled_program",RTLD_LAZY);
    if (!handle) {
        /* fail to load the library */
        fprintf(stderr,"Error: %s\n",dlerror());
        return EXIT_FAILURE;
    }
    *(void**)(&func_in_question) = dlsym(handle,"function_I_would_like_to_use");
    if (!func_in_question) {
        /* no such symbol */
        fprintf(stderr,dlerror());
        dlclose(handle);
        return EXIT_FAILURE;
    }
    func_in_question(argv[1]);
    dlclose(handle);
    return EXIT_SUCCESS;
}

返回未定义符号错误

我将不胜感激,a) 我可能无法访问此符号的任何其他原因? b) 如果我对依赖的半生不熟的怀疑几乎是有道理的!

编辑:从上面链接的原始问题中复制错误消息:

Error: ./compiled_program: undefined symbol: function_I_would_like_to_use

和原始 nm --defined-only ./compiled_program输出 问题:

    0000000000200d98 d _DYNAMIC
    0000000000200f88 d _GLOBAL_OFFSET_TABLE_
    0000000000000ba0 R _IO_stdin_used
    0000000000000d64 r __FRAME_END__
    0000000000000bfc r __GNU_EH_FRAME_HDR
    0000000000201010 D __TMC_END__
    0000000000201010 B __bss_start
    0000000000201000 D __data_start
    00000000000007d0 t __do_global_dtors_aux
    0000000000200d90 t __do_global_dtors_aux_fini_array_entry
    0000000000201008 D __dso_handle
    0000000000200d88 t __frame_dummy_init_array_entry
    0000000000200d90 t __init_array_end
    0000000000200d88 t __init_array_start
    0000000000000b90 T __libc_csu_fini
    0000000000000b20 T __libc_csu_init
    0000000000201010 D _edata
    0000000000201018 B _end
    0000000000000b94 T _fini
    0000000000000660 T _init
    0000000000000710 T _start
    0000000000201010 b completed.7696
    0000000000201000 W data_start
    0000000000000740 t deregister_tm_clones
    0000000000000810 t frame_dummy
    0000000000000a92 T main
    000000000000081a T function_I_would_like_to_use                 \\ << this one
    0000000000000780 t register_tm_clones

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)