How would a loaded library function call a symbol in the main application?

后端 未结 3 1383
春和景丽
春和景丽 2020-12-25 09:15

When loaded a shared library is opened via the function dlopen(), is there a way for it to call functions in main program?

3条回答
  •  半阙折子戏
    2020-12-25 09:31

    Code of dlo.c (the lib):

    #include 
    
    // function is defined in main program
    void callb(void);
    
    void test(void) {
        printf("here, in lib\n");
        callb();
    }
    

    Compile with

    gcc -shared -olibdlo.so dlo.c
    

    Here the code of the main program (copied from dlopen manpage, and adjusted):

    #include 
    #include 
    #include 
    
    void callb(void) {
        printf("here, i'm back\n");
    }
    
    int
    main(int argc, char **argv)
    {
        void *handle;
        void (*test)(void);
        char *error;
    
        handle = dlopen("libdlo.so", RTLD_LAZY);
        if (!handle) {
            fprintf(stderr, "%s\n", dlerror());
            exit(EXIT_FAILURE);
        }
    
        dlerror();    /* Clear any existing error */
    
        *(void **) (&test) = dlsym(handle, "test");
    
        if ((error = dlerror()) != NULL)  {
            fprintf(stderr, "%s\n", error);
            exit(EXIT_FAILURE);
        }
    
        (*test)();
        dlclose(handle);
        exit(EXIT_SUCCESS);
    }
    

    Build with

    gcc -ldl -rdynamic main.c
    

    Output:

    [js@HOST2 dlopen]$ LD_LIBRARY_PATH=. ./a.out
    here, in lib
    here, i'm back
    [js@HOST2 dlopen]$
    

    The -rdynamic option puts all symbols in the dynamic symbol table (which is mapped into memory), not only the names of the used symbols. Read further about it here. Of course you can also provide function pointers (or a struct of function pointers) that define the interface between the library and your main program. It's actually the method what i would choose probably. I heard from other people that it's not so easy to do -rdynamic in windows, and it also would make for a cleaner communication between library and main program (you've got precise control on what can be called and not), but it also requires more house-keeping.

提交回复
热议问题