Dlsym-like functionality for dynamically loaded code?

I know how to use dlsym()to search for characters entered using a string - when these characters are exported by the shared library that I dlopen()edited. But what about another code? Just the object code that I linked statically. Is it possible to somehow find characters?

Notes:

  • If this helps, make any reasonable assumptions about the compilation and linking process (for example, which compiler, debugging information, PIC code, etc.).
  • I'm more interested in a solution other than the OS, but if that matters: Linux.
  • Solutions related to pre-registration of functions are not relevant. Rather, perhaps they are, but I would prefer to avoid this.
+4
source share
1 answer

You can simply use dlsym()for this purpose. You just need to export all the characters to a dynamic symbol table. Link the binary to gcc -rdynamicfor this.

Example:

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

void foo (void) {
    puts("foo");
}

int main (void) {
    void (*foo)(void) = dlsym(NULL, "foo");
    foo();
    return 0;
}

Compile with: gcc -rdynamic -O2 dl.c -o dl -ldl

$ ./dl
foo
$
+3
source

Source: https://habr.com/ru/post/1625040/


All Articles