Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

module

dlopen

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

like image 564
Nikron Avatar asked Dec 25 '08 01:12

Nikron


1 Answers

Code of dlo.c (the lib):

#include <stdio.h>

// 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 <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

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.

like image 108
Johannes Schaub - litb Avatar answered Oct 18 '22 20:10

Johannes Schaub - litb