OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Jun 12, 2024 11:53 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Linking And Loading ... In Userspace?
PostPosted: Sun Jan 28, 2024 9:22 am 
Offline
Member
Member
User avatar

Joined: Mon Sep 21, 2020 9:51 am
Posts: 102
Location: Aboard the Enterprise
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

_________________
USS Enterprise NCC-1701,
The Final Frontier,
Space,
The Universe

Live Long And Prosper

Slava Ukraini!
Слава Україні!


Top
 Profile  
 
 Post subject: Re: Linking And Loading ... In Userspace?
PostPosted: Sun Jan 28, 2024 11:07 am 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 406
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


Yes.

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:


Andreas Kling (SerenityOS) also has an offshoot Javascript library with JIT, along with videos of hacking on the JIT library that you may find useful:

https://youtube.com/playlist?list=PLMOp ... kfUlLgq8dO


Top
 Profile  
 
 Post subject: Re: Linking And Loading ... In Userspace?
PostPosted: Fri Feb 09, 2024 1:07 am 
Offline

Joined: Sat Jan 06, 2024 2:55 am
Posts: 11
Location: India
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group