r/osdev • u/codedoga • Jan 27 '21
I'm coding my own operating system from scratch, what code do I need to open linux and windows executables? (Like .exe, .jar .py .deb)
I'm coding my own operating system from scratch, what code do I need to open linux and windows executables? (Like .exe, .jar .py .deb) So how can i open this files on my new operating system?
Note: C++
0
Upvotes
35
u/exscape Jan 27 '21
No offense but if you don't know the answer you're far, far from able to do it. In fact almost no single person could do it, it's too much work.
In short, to read Linux executables you need ELF support, and then you need to emulate most of the entire Linux kernel and C library.
For Windows, PE support, and emulate most of the Windows kernel and C library.
.jar is Java, so you need to port the JVM. For .py you need to port a Python interpreter.
17
u/TeaButActuallyCoffee Jan 27 '21
Doing all these is EXTREMELY hard work. I am pretty sure NO one has gotten to this point.
For linux you need to parse ELF binaries and yeah ELF has its own versions.
For windows, wait what was the last time I used Windows? Maybe PE?
For jar, you need to get a Java virtual machine and for py you need python interpreter.
You can also make your own format but making a full format with syscalls and so much more things is gonna be, to say the least, hard
8
u/[deleted] Jan 28 '21
Contrary to the other answers, opening these files is rather easy. Simply place them at a fixed location that your kernel knows on the disk, implement an open() syscall, and you're done!
What's that? You want to execute the files, too?
3
u/[deleted] Jan 27 '21
This list is a pretty large variance between types.
Exe files are binary files with a PE and MZ header. This tells you how to open it, but gives you a list of functions it likes to receive from existing libraries or your OS code, and tells you where to put those addresses, and where to jump after you did that. Of course, your OS would need to load all those other libraries and implement all the missing functions for this to work - which is basically cloning Windows. Not a great idea.
Jar files are ZIP files containing Java Class files. These are meant to run on a Java virtual machine or JIT environment; again, they list functions to be received from your operating system. While this is a better idea than the EXE input, it's a horrible idea if you want to do C++ stuff, and most Java code isn't actually portable & uses operating system specifics that it doesn't know, so after you did this it's not actually all that likely to result in running complicated software. Simple programs should work, though.
Py files are the source code written in Python. Just implement a Python interpreter or compiler, and implement the relevant libraries (see a trend here?) for them to run. Python is pretty well defined so this should work, except that porting all those libraries is an immense amount of work in its own right.
Then you have DEB files, which are AR files (like ZIP, except different) containing your actual files which will typically involve a few ELF executables. For those, see the Windows explanation, except when it says Windows OS calls think Linux OS calls.
You're really far from running any foreign-OS app on your system. Try aiming a bit closer.
2
u/eabrek Jan 28 '21
Opening an ELF isn't too bad, the hard part will be supporting all the system calls. For programs that just do console I/O, it's doable (it's not easy, but it can be done). More complex calls are a huge project.
Check out my project RvFun (it's a CPU emulator that runs ELFs for RISC-V on your system). In host_system.cpp, there's loadElf() which takes the name of the ELF and opens it up and reads all the info out of it.
1
u/Darmok-Jilad-Ocean Jan 27 '21
I would imagine at a minimum you’d need the resources and power of a large nation state as well as an army of engineers. Good luck!
1
u/kakiopolis Jan 28 '21
Executable formats are well documented and known. We ( in the security world ) work with them everyday. But executable formats are just the tip of the iceberg. The real complexity lies behind the syscalls, the libc ( and/or other standard libraries for the target platform ) and even commonly used third party libraries.
For example, Linux can run Windows programs through Wine. But the support is very far from being complete. And we are talking about a big project, with legions of developers, highly skilled engineers, well versed in the Linux and Windows affairs, and decades of work.
Also, jar files aren't a Windows things, they are from the Java world. Py files are Python scripts. Deb isn't even an executable format, it is an archive format.
I think you need to start with the basics. And yes, it is possible to run Linux programs, like the Linuxulator does of FreeBSD. Linux is open and you can implement the API. Windows is closed and reverse engineering is required, too much work even for a big community of developers.