cdynamic-linkinglddlopen

dlopen from memory?


I'm looking for a way to load generated object code directly from memory.

I understand that if I write it to a file, I can call dlopen to dynamically load its symbols and link them. However, this seems a bit of a roundabout way, considering that it starts off in memory, is written to disk, and then is reloaded in memory by dlopen. I'm wondering if there is some way to dynamically link object code that exists in memory. From what I can tell there might be a few different ways to do this:

  1. Trick dlopen into thinking that your memory location is a file, even though it never leaves memory.

  2. Find some other system call which does what I'm looking for (I don't think this exists).

  3. Find some dynamic linking library which can link code directly in memory. Obviously, this one is a bit hard to google for, as "dynamic linking library" turns up information on how to dynamically link libraries, not on libraries which perform the task of dynamically linking.

  4. Abstract some API from a linker and create a new library out its codebase. (obviously this is the least desirable option for me).

So which ones of these are possible? feasible? Could you point me to any of the things I hypothesized existed? Is there another way I haven't even thought of?


Solution

  • There is no standard way to do it other than writing out the file and then loading it again with dlopen().

    You may find some alternative method on your current specific platform. It will be up to you to decide whether that is better than using the 'standard and (relatively) portable' approach.

    Since generating the object code in the first place is rather platform-specific, additional platform-specific techniques may not matter to you. But it is a judgement call — and in any case, it depends on there being a non-standard technique, which is relatively improbable.