Functionally, a binary (as in ELF binary which can be executed from the shell) is somewhat similar to a library in that both are files containing machine code for a particular target ABI in binary form.
Differences would be:
- An executable binary has one well-defined entry point (
main()
in C) with a well-defined signature (arguments and return value), whereas a dynamic library can have any number of entry points with arbitrary names and signatures, each of which must be known to callers that want to use it. - An executable binary runs in its own, newly created process, whereas dynamic library code runs in the process (and initially also the thread) of the caller.
On the implementation side, though, I have no idea how different the file formats are.
However, it should theoretically be possible to take an executable binary and transform it into a library which exports one single function, int main (int argc, char *argv[])
.
From the functional side, the only potential obstacle I would see is that an executable binary expects to be the “boss” of the process, whereas a library function clearly is not – if the “boss” were to expect exclusive control over certain resources (such as placing the first global variable at a hard-coded memory address rather than allocating space dynamically), this would cause conflicts and result in hard-to-predict behavior.
Question: Is there any tool which can convert a compiled ELF binary into a dynamic library which can be called from, and run inside, another process?