Valhalla Legends Forums Archive | General Programming | Executing Data In Libraries

AuthorMessageTime
FrostWraith
I was thinking about trying something new and wasn't sure if this is at all possible.  I was thinking about putting multiple executables in a single file and was wondering if it was possible to read the binary data of the file and execute it without having to extract it first.

Example:

compilation.bin contains 3 files:
prog1.exe ; prog2.exe ; prog3.exe

Say the length of the first file would be 20,000 bytes.  Could I do something along the lines of:
Execute (compilation.bin, 1, 20000)

This structure would be somewhat similar to blizzards mpq archive.

All help appreciated!
February 8, 2007, 10:03 PM
Myndfyr
This is very similar to this thread I posted some time ago.  It's probably not feasible for what you're hoping for.
February 8, 2007, 10:54 PM
JoeTheOdd
[quote author=FrostWraith link=topic=16275.msg164326#msg164326 date=1170972232]
I was thinking about trying something new and wasn't sure if this is at all possible.  I was thinking about putting multiple executables in a single file and was wondering if it was possible to read the binary data of the file and execute it without having to extract it first.

Example:

compilation.bin contains 3 files:
prog1.exe ; prog2.exe ; prog3.exe

Say the length of the first file would be 20,000 bytes.  Could I do something along the lines of:
Execute (compilation.bin, 1, 20000)

This structure would be somewhat similar to blizzards mpq archive.

All help appreciated!
[/quote]

I think with DLL's there is a away to map them to the program's memory and execute them, but for EXE's, I don't know.
February 9, 2007, 11:11 PM
JoeTheOdd
Now that I think about it, you might be able to load the entire EXE into a byte array and far jump to it's pointer, pretending it's a function (the program's main() should return a value upon close too, so it'd be a true int function), but your host program would wait for the loaded program to return before the method finishes causing it to freeze up. Also, I'm pretty sure most C++ compilers impose some sort of limit on how big of a byte array you can declare too (IE: Compiler rule of no arrays longer than a half MB or so), but that could easily be defeated by declaring enough arrays next to eachother in memory.

It'd be REALLY ugly, but I think it's possible.
February 12, 2007, 10:28 PM
Myndfyr
[quote author=Joe[x86] link=topic=16275.msg164661#msg164661 date=1171319288]
Now that I think about it, you might be able to load the entire EXE into a byte array and far jump to it's pointer, pretending it's a function (the program's main() should return a value upon close too, so it'd be a true int function), but your host program would wait for the loaded program to return before the method finishes causing it to freeze up. Also, I'm pretty sure most C++ compilers impose some sort of limit on how big of a byte array you can declare too (IE: Compiler rule of no arrays longer than a half MB or so), but that could easily be defeated by declaring enough arrays next to eachother in memory.

It'd be REALLY ugly, but I think it's possible.
[/quote]

1.) You don't need to declare a byte array in C or C++.  You can simply malloc() it.
2.) When you read data like that you don't get to execute it directly.  You'd have to do change its execution permissions.
3.) When Windows loads binaries, it performs automatic relocation by remapping pointers according to the image's base address.  For instance, if a binary is compiled to execute at 0x400000, but it's loaded at virtual address 0x480000, Windows will automatically offset all pointers in the image to the base address.  There isn't really a public operating system API to do this.
February 13, 2007, 5:42 AM
St0rm.iD
...and wouldn't you have to take care of dynamically linking everything as well?
February 14, 2007, 2:51 AM
Myndfyr
[quote author=Banana fanna fo fanna link=topic=16275.msg164773#msg164773 date=1171421493]
...and wouldn't you have to take care of dynamically linking everything as well?
[/quote]

Oh yeah, there's that too.
February 14, 2007, 8:01 AM
Arta
I think you can do this. You just need to write your own loader. ImageHlp can do a lot of the heavy lifting for you (like making sure all the sections are mapped to the correct memory locations). I'm not sure if it will do linking for you. I don't know if it does base address relocation either, but it shouldn't be too hard: see the IMAGE_DIRECTORY_ENTRY_BASERELOC data directory.

In any event, it's completely possible -- but not trivial. Depending on how much of the work ImageHlp can do for you, it might be reasonably easy.
February 14, 2007, 12:15 PM
Skywing
I think that you are looking for CreateFileMapping(...SEC_IMAGE...) + MapViewOfFile(...) and not imagehlp.
February 14, 2007, 2:57 PM
Arta
Hmm, I was referring to MapAndLoad, but it's been a while since I looked at these things.
February 20, 2007, 12:43 AM

Search