3

How can I add a symbol to process symbol table dynamically on linux? For example, suppose I parse a script and determine that it has some methods which I then want to add into global address space so that they can be accessed by shared libraries loaded afterwards. What I then want to do is add a function pointer to a stub function that will call the script function and return the result (in C). But I want to say "add 'foo' pointing to &mygenericstub to global symbol table".. so when I then do dlopen() on an .so file with RTLD_GLOBAL flag, it will be able to call 'foo' as though it was a normal C function in the global namespace..

The idea is rather simple: to extend functionality of dynamic linker for that single process to also include names from scripts (but not just having an entry point function that takes a string argument for the script function to be called - but rather making the dynamic linking process transparent to other shared objects that may be loaded later). The idea would be not to modify existing table, but rather to map a new table as an extension to existing one (maybe this can somehow be done using mmap but I don't know what to map and how to extend it). Basically something similar to dlopen with the "global" option that works for dynamic names would be interesting.

user2826084
  • 517
  • 5
  • 11
  • Possible duplicate: http://stackoverflow.com/questions/30147018/dynamical-modify-symbol-table-at-runtime-in-c –  Feb 13 '16 at 23:35

1 Answers1

4

This looks quite cumbersome. You should probably change your code in order to lookup the symbol directly from your script interpreteur/VM.

but…

suppose I parse a script and determine that it has some methods which I then want to add into global address space so that they can be accessed by shared libraries loaded afterwards.

AFAIK, there is no API exposed by the dynamic linker to register additional symbols. However, what you can is dlopen a new shared library whith those symbols:

  1. dynamically generate .so file with a symbol for each function you want to expose;

  2. each of those symbols reference a PLT-like pattern (the code fetch a GOT like entry and jump to it);

  3. dlopen RTLD_GLOBAL this .so file;

  4. fill the GOT-like variables;

  5. you can now dlopen additional .so which will call to the PLT-like fuction and jump through the GOT-like variable to the real function.

Code of the shared object:

    .text
    .globl  main
    .type   main, @function
foo:
    .cfi_startproc
    jmp *foo_got_alike(%rip)
    .cfi_endproc
.bss
    .align 8
    .type   foo_got_alike, @object
    .size   foo_got_alike, 8
foo_got_alike:
    .zero   8

This suppose you have managed to generate a suitable implementation for0 foo using some kind of JIT.

ysdx
  • 8,889
  • 1
  • 38
  • 51