hrszpuk / rvm

A simplistic bytecode virtual machine.
MIT License
4 stars 0 forks source link

[Feature Request] dlopen integration #30

Closed Khhs167 closed 7 months ago

Khhs167 commented 7 months ago

Hi! I noticed this project has been going well underway, however there's one thing I'm wondering about, and that's dlopen.

I'm not an expert on how you envision RVM working, but the ability to load and call external libraries would open the entire project up to near limitless possibilities.

I have a simple idea for how this could be done(written in some pseudo-assembler thing):

.CODE ; This is just me coming from ca65 assembler

push libc_name ; We push the external binary name

; ldexl pops a location from stack, reads as string, loads the library and stores
; the library handle in the location specified via argument 1
ldexl libc_handle

; We push the libc library handle
push libc_handle
push libc_malloc_name
; ldexf is like ldexl but it loads a function from the library.
; Top of the stack is the function name string pointer, 2nd in stack is
; the library which we're loading from.
ldexf libc_malloc_handle

; We push argument #1 to malloc
push 16
; We call malloc via ldexc, which is like call, but for external methods.
ldexc libc_malloc_handle
; Then we get our pointer as a return value on the stack or smth
pop ; I guess we need an argument or smth?

.RODATA ; Read-only data or smth idk

; libc_name would just be a label with the location of the string "libc.so"
libc_name: .str "libc.so"
; This is just 4 bytes for storing a handle(could just be a pointer value from C code)
libc_handle: .bytes 4
; Just like above but for the malloc function
libc_malloc_name: .str "malloc"
libc_malloc_handle: .bytes 4
Khhs167 commented 7 months ago

It'd be phenomenal if ldex* handles rvm libraries too(if possible).

I don't know your plans, but y'know...

hrszpuk commented 7 months ago

Yeah I think external library support would be swell. I'm not sure exactly the best way to implement it just yet.