Question

I am writing my own programming language just for the fun of it. Currently I have a stacked based Virtual Machine , an intermediate language / assembly language and a high level compiler. So anyways everything works but I need to dynamically link compiled code libraries. The problem is my assembler needs to know the address of individual methods from the external libraries. How exactly does a C++ compiler or a java compiler handle this issue?

Was it helpful?

Solution

For a statically linked executable, the linker is responsible for matching up function calls with actual addresses. One object file will contain a symbolic reference (meaning a name, not an address) to a function somewhere else, and the linker will match that up with a symbol definition in some other object file or library at link time. Using this method would require that you generate native machine code into an object file that the linker will understand.

The other option, which might be more suitable for your VM situation, is to use dynamic linking where your program gets the address of linked functions at runtime. The exact method to do this is platform dependent; on Windows you would use LoadLibrary and GetProcAddress, while on Unix style platforms you would use dlopen and dlsym.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top