0

The background of the issue is in an already answered question, here

I have a symbol that I want to use in a shared library but despite being listed as a T defined symbol via nm, F (function) via objdump, when I try to access it using dlopen() or via Python (ctypes.CDLL), it seemingly does not exist or is not defined.

I was reading here that this could be related to dependencies.

Output of 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)

Output of 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

With my limited knowlege of C, I am unable to tell if any of these might be unresolved dependencies and where to look for which libraries to load, as the answer suggests, to include them. Is it as simple as finding the library and adding an #include <...> or do I have to locate the library and load it with dlopen()? The answer seems to suggest the latter. I appreciate that I am completely flailing in the dark here!

My current dlopen code:

#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, "Error: %s\n", dlerror());
        dlclose(handle);
        return EXIT_FAILURE;
    }
    func_in_question(argv[1]);
    dlclose(handle);
    return EXIT_SUCCESS;
}

comes back with the undefined symbol error.

I would be grateful for any pointers on, a) any other reasons why I might not be able to access this symbol? and b) if my half-baked suspicion about dependencies is anywhere near to making sense!

EDIT: copying error message from original question linked above:

Error: ./compiled_program: undefined symbol: function_I_would_like_to_use

and the output of nm --defined-only ./compiled_program from original question:

    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
rici
  • 234,347
  • 28
  • 237
  • 341
nihilok
  • 1,325
  • 8
  • 12
  • Please provide a [mre] with the full error message – Alan Birtles Mar 11 '21 at 14:09
  • `Error: ./compiled_file: undefined symbol: function_I_would_like_to_use` – nihilok Mar 11 '21 at 14:10
  • Is `compiled_program` a dynamic, shared object? – stark Mar 11 '21 at 14:54
  • I doubt whether this has anything to do with dependencies. But I note that you have no clear idea about which call failed. I'd suggest changing your error format strings to make this clear: one should be `"Error in dlopen: %s\n"` and the other `"Error in dlsym: %s\n"`, or something like that. – rici Mar 11 '21 at 14:59
  • @stark, yes I believe so according to output of `file`: `./compiled_file: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=cf62f3afa6f99f98a863d44932d2e 0f9f8594e71, not stripped` @rici, thanks for the tip but the error is "undefined symbol" and mentions the symbol called in dlsym, so it must have been that. – nihilok Mar 11 '21 at 23:33
  • Use `extern "C"` when you define your functions. (A note about C++: it doesn't work well together with $X -- In this case X=dlopen/dlsym) – Lorinczy Zsigmond Mar 12 '21 at 13:18

0 Answers0