Would it be possible to have a userspace program (on a unix os), to do the reading of the executable into memory yourself, and then tell the OS to skip that step and run it? For this (admittedly strangely specific) use case, the program would generate the executable itself directly into memory.
I figure an option could be to make some sort of pseudo-fs and trick the os into thinking it's reading from a file, but it's a very clunky way of doing things.
Thanks
Linking And Loading ... In Userspace?
- PavelChekov
- Member
- Posts: 113
- Joined: Mon Sep 21, 2020 9:51 am
- Location: Aboard the Enterprise
Linking And Loading ... In Userspace?
USS Enterprise NCC-1701,
The Final Frontier,
Space,
The Universe
Live Long And Prosper
Slava Ukraini!
Слава Україні!
The Final Frontier,
Space,
The Universe
Live Long And Prosper
Slava Ukraini!
Слава Україні!
-
- Member
- Posts: 422
- Joined: Tue Apr 03, 2018 2:44 am
Re: Linking And Loading ... In Userspace?
Yes.PavelChekov wrote:Would it be possible to have a userspace program (on a unix os), to do the reading of the executable into memory yourself, and then tell the OS to skip that step and run it? For this (admittedly strangely specific) use case, the program would generate the executable itself directly into memory.
I figure an option could be to make some sort of pseudo-fs and trick the os into thinking it's reading from a file, but it's a very clunky way of doing things.
Thanks
You mmap the portion of the file that has the executable code as executable (PROT_EXEC in mmap), do whatever relocations are required to make the code runnable at the address it is located, then jump to it.
This is basically what a dynamic linker is doing. When a dynamically linked ELF binary is loaded, the binary specifies an "interpreter", which the kernel loads and jumps to instead. The interpreter (typically something like /lib64/ld-linux-x86-64.so.2 on Linux) then loads in the actual binary (if required) and any libraries pulled in as dependencies, using mmap above, fixes up what is required to dynamic link functions and data, and jumps to the binary entry point.
But it sounds like what you're doing is some sort of Just In Time compilation (JIT), which you can do with correctly protected memory (mprotect with PROT_EXEC again). Language VMs do this all the time.
I couldn't recommend a labguage VM to look at in inspiration, but you can browse the source on any of the following open source language VMs:
- https://en.wikipedia.org/wiki/LuaJIT
- https://en.wikipedia.org/wiki/V8_(JavaScript_engine)
- https://en.wikipedia.org/wiki/SpiderMonkey
https://youtube.com/playlist?list=PLMOp ... kfUlLgq8dO
-
- Posts: 11
- Joined: Sat Jan 06, 2024 2:55 am
- Libera.chat IRC: @freenode-nf1
- Location: India
- Contact:
Re: Linking And Loading ... In Userspace?
Yes, it's possible to load and run an executable directly from memory on Unix like systems. This technique is occasionally utilized in malware and security research. It involves creating a program to load the executable into memory and commence its execution.