Dlopen: Is it possible to catch unresolved characters by manually resolving them as they happen?

Is it possible to catch unresolved symbol references when they occur, so that the function is called to try to resolve the symbol as necessary? Or can you add new characters to the dynamic symbol table at runtime without creating a library file and dlopen'ing? I am on GNU / Linux using GCC. (Portability to other Unixes would be nice, but that is not the main problem.)

Thanks in advance!

Edit: I had to elaborate on what I'm trying to do. I want to write an interpreter for a programming language that is expected to support both compiled (dlopen'ed) and interpreted modules. I wanted the calls from the compiled module to functions defined elsewhere to be resolved by the linker to avoid finding a function on every call, but calls to the interpreted code would remain unresolved. I wanted to capture these calls so that I could call the corresponding interpreted function if necessary (or report an error if the function does not exist).

+4
source share
2 answers

If you know which characters are missing, you can only write a library with them and LD_PRELOAD before running the application.

If you do not have a list of missing characters, you can detect them with "nm" or "objdump" in binary format, and based on this, write a script that will create a library with missing characters before executing the application, and then LD_PRELOAD as well.

Alternatively, you can use gdb to enter new โ€œcodeโ€ into applications so that functions indicate what you need.

Finally, you can also override some ld.so functions to detect missing characters and do something with them.

But in any case, if you could explain what you are trying to accomplish, it would be easier to provide the right solution.

+2
source

I assume that the problem you are trying to solve is when you dlopen and start using a loadable module, and then suddenly crash due to unresolved characters. If so, this is the result of lazy binding , and you can disable it by exporting LD_BIND_NOW=1 (or any value, if set) in the environment. This ensures that all characters can be resolved before dlopen , and if not, the dlopen operation will fail, allowing the situation to be handled gracefully.

+2
source

All Articles