473,846 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

1)Executing by reading from executable 2)Compileing in a program

Two questions.

1)Is there any way that I can read from an executable and then execute
what I have read.
EXAMPLE:

text
text
this is more
text

:::::START EXE CODE:::
/* executable code in here */
:::::END EXE CODE:::

text text
yep
yeah
text
2)Is there anyway that I can modify the source code of a compiler so
that I can include it in another program to compile other files in that
program and place the output of the compiled files into a large file in
a location specified by an argument to the call of the compiler. Here
is an example.

##MY PSUDO FILE SYSTEM##
$$fl.exe
/* exe code for fl.exe */
&&%

Now I use the compiler that is included within the program. Say at my
programs prompt I enter:(where cc is the compilers name)
:: cc myprog.c -o myprog.exe

This would result in, if all was well:

##MY PSUDO FILE SYSTEM##
$$fl.exe
/* exe code for fl.exe */
&&%
$$myprog.exe
/* exe code for myprog.exe*/
&&%

I'm not sure if this is possible or not but I'm sure some people will
understand what I'm saying, and perhaps some guru will know if it can
be done.
Nori

May 13 '06 #1
14 1680
no*********@gma il.com wrote:
Two questions.

1)Is there any way that I can read from an executable and then execute
what I have read.
EXAMPLE:
<snip>

Not in standard C. Your implementation may provide some mechanism for
doing this or may prevent you from doing this.
2)Is there anyway that I can modify the source code of a compiler so
that I can include it in another program to compile other files in that
program and place the output of the compiled files into a large file in
a location specified by an argument to the call of the compiler. Here
is an example.


<snip>

Maybe yes, maybe no. It depends on whether you have the source code for
the compiler and whether you are a good enough programmer. However, it
is probably the wrong solution to whatever problem you are actually
trying to solve.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 13 '06 #2
On 2006-05-13, no*********@gma il.com <no*********@gm ail.com> wrote:
Two questions.

1)Is there any way that I can read from an executable and then execute
what I have read.
Interesting idea; it is possible on a machine on which code and data are
stored in the same memory (I think this is called a "von Neumann
machine")-- most modern desktop machines are like that. So I gave it a
go.

First I made a file called blah.c with this in it:

int add(a, b)
{
return a + b;
}

and compiled it to blah.o. [this would be called blah.obj on some
systems].

Then I disassembled blah.o and found the actual code of "add" was 11
bytes long and started at offset 0x34L:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 45 0c mov 0xc(%ebp),%eax
6: 03 45 08 add 0x8(%ebp),%eax
9: 5d pop %ebp
a: c3 ret

So then I just loaded the code directly into an array in another
program, cast the buffer to the proper function type (casting char * to
to a function type I'm pretty sure is "undefined behaviour", but I
thought it might work anyway), and called it:

#include <stdio.h>
#include <stdlib.h>

typedef int (*add_t)(int, int);

add_t load_add(void)
{
FILE *fp = fopen("./blah.o", "rb");
char *code = malloc(11);

fseek(fp, 0x34L, SEEK_SET);
fread(code, 1, 11, fp);

fclose(fp);

return (add_t) code;
}

int main(void)
{
add_t add = load_add();
int result = add(4, 5);

printf("%d\n", result);

return 0;
}

and it printed out 9!

Maybe you can use something like this, depending on what you're trying
to do.
2)Is there anyway that I can modify the source code of a compiler so
that I can include it in another program to compile other files in that
program and place the output of the compiled files into a large file in
a location specified by an argument to the call of the compiler. Here
is an example.

##MY PSUDO FILE SYSTEM##
$$fl.exe
/* exe code for fl.exe */
&&%

Now I use the compiler that is included within the program. Say at my
programs prompt I enter:(where cc is the compilers name)
:: cc myprog.c -o myprog.exe

This would result in, if all was well:

##MY PSUDO FILE SYSTEM##
$$fl.exe
/* exe code for fl.exe */
&&%
$$myprog.exe
/* exe code for myprog.exe*/
&&%

I'm not sure if this is possible or not but I'm sure some people will
understand what I'm saying, and perhaps some guru will know if it can
be done.


This part I don't really understand I'm afraid. I gather from your other
posts that you're trying to do a kind of hosted OS, one of the features
of which is its own filesystem that appears as a single file to the
hosted OS. The mini-OS itself appears to the host OS as a single running
process, and now it sounds like you want the mini-OS to be able to load
executables from its own filesystem and run them.

The executables it runs are in native compiled code, however.

This makes me think you might be better to work at the level of .o (or
..obj), than .exe. An exe is likely to do all kinds of things required to
start up a real process on the hosted OS, but it sounds like you don't
want that, you just want to run some native code out of the middle of it
somewhere.

If you do use .obj files you will need to do your own "linking", but
that might be part of what the mini-OS does... I don't know.
May 13 '06 #3

Ben C wrote:
On 2006-05-13, no*********@gma il.com <no*********@gm ail.com> wrote:
Two questions.

1)Is there any way that I can read from an executable and then execute
what I have read.


Interesting idea; it is possible on a machine on which code and data are
stored in the same memory (I think this is called a "von Neumann
machine")-- most modern desktop machines are like that. So I gave it a
go.

First I made a file called blah.c with this in it:

int add(a, b)
{
return a + b;
}

and compiled it to blah.o. [this would be called blah.obj on some
systems].

Then I disassembled blah.o and found the actual code of "add" was 11
bytes long and started at offset 0x34L:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 45 0c mov 0xc(%ebp),%eax
6: 03 45 08 add 0x8(%ebp),%eax
9: 5d pop %ebp
a: c3 ret

So then I just loaded the code directly into an array in another
program, cast the buffer to the proper function type (casting char * to
to a function type I'm pretty sure is "undefined behaviour", but I
thought it might work anyway), and called it:

#include <stdio.h>
#include <stdlib.h>

typedef int (*add_t)(int, int);

add_t load_add(void)
{
FILE *fp = fopen("./blah.o", "rb");
char *code = malloc(11);

fseek(fp, 0x34L, SEEK_SET);
fread(code, 1, 11, fp);

fclose(fp);

return (add_t) code;
}

int main(void)
{
add_t add = load_add();
int result = add(4, 5);

printf("%d\n", result);

return 0;
}

and it printed out 9!

Maybe you can use something like this, depending on what you're trying
to do.
2)Is there anyway that I can modify the source code of a compiler so
that I can include it in another program to compile other files in that
program and place the output of the compiled files into a large file in
a location specified by an argument to the call of the compiler. Here
is an example.

##MY PSUDO FILE SYSTEM##
$$fl.exe
/* exe code for fl.exe */
&&%

Now I use the compiler that is included within the program. Say at my
programs prompt I enter:(where cc is the compilers name)
:: cc myprog.c -o myprog.exe

This would result in, if all was well:

##MY PSUDO FILE SYSTEM##
$$fl.exe
/* exe code for fl.exe */
&&%
$$myprog.exe
/* exe code for myprog.exe*/
&&%

I'm not sure if this is possible or not but I'm sure some people will
understand what I'm saying, and perhaps some guru will know if it can
be done.


This part I don't really understand I'm afraid. I gather from your other
posts that you're trying to do a kind of hosted OS, one of the features
of which is its own filesystem that appears as a single file to the
hosted OS. The mini-OS itself appears to the host OS as a single running
process, and now it sounds like you want the mini-OS to be able to load
executables from its own filesystem and run them.

The executables it runs are in native compiled code, however.

This makes me think you might be better to work at the level of .o (or
.obj), than .exe. An exe is likely to do all kinds of things required to
start up a real process on the hosted OS, but it sounds like you don't
want that, you just want to run some native code out of the middle of it
somewhere.

If you do use .obj files you will need to do your own "linking", but
that might be part of what the mini-OS does... I don't know.

YES! Finally somebody understands what it is that I'm trying to do.
You have hit the nail right on the head and this has been very helpful.
I had no idea how to go about the execuatble portion of this and I
thank you greatly for your help. This is likely exactly what I will
do.
As for the linking...I'm not exactly sure how to go about that, but it
apears that you have done am I correct? I'm not sure how I could write
a program that would link .o files makeing them executables, however,
scince I don't really care what the operating system that my mini-OS is
running on has to say about much of anything I'm not sure if it would
make much of a differance. Or would it? I'm not intirely sure I know
what I'm getting myself into here :p.
Anyway if you could explain what you ment by the mini-OS linking and
also about if it would be nesisary to have .exe files at all it would
be much apreciated by me.
Nori

P.S. This is the most helpful post I have gotten thus far relating to
my project.

May 13 '06 #4
Ben C wrote:
On 2006-05-13, no*********@gma il.com <no*********@gm ail.com> wrote:
Two questions.

1)Is there any way that I can read from an executable and then execute
what I have read.


Interesting idea; it is possible on a machine on which code and data are
stored in the same memory (I think this is called a "von Neumann
machine")-- most modern desktop machines are like that. So I gave it a
go.


<snip>

It's not always possible. The OS might prevent it (mark all pages that
are writeable by the application as not executable). Or you might have
to do relocation which involves understanding the format of the file.

On the other hand, with a few tricks it is possible on some processors
which have separate program and data address spaces.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 13 '06 #5
In article <7s************ @news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
It's not always possible. The OS might prevent it (mark all pages that
are writeable by the application as not executable).


This is rare in current operating systems, since many programs rely on
being able to do it (in fact, on many versions of unix, all programs
do it). Even if it is disabled by default, there is likely to be a
system call to allow it.

-- Richard
May 13 '06 #6
>1)Is there any way that I can read from an executable and then execute
what I have read.
You can write an emulator for the target CPU and hardware, then
emulate the execution of the program. As long as the specs for the
hardware are available, you should be able to do this, although I
didn't say it would be as fast as the real thing. (For some
processors such as the Z80, there are emulators using today's
hardware that are much faster than the real thing ever got).

You can also provide convenience facilities for dealing with the
emulation, like loading an "emulated ROM" with a just-compiled
program, memory and register dumps and patching, stack traces,
symbolic debugging, breakpoints, emulated I/O devices, etc.

....2)Is there anyway that I can modify the source code of a compiler so
that I can include it in another program to compile other files in that
program and place the output of the compiled files into a large file in
a location specified by an argument to the call of the compiler. Here
is an example.


It would seem simpler to compile the code (if necessary using a
cross-compiler), get an executable, and provide a utility to copy
the executable into your emulated file system, possibly doing format
conversion of the executable if needed. If you must, you can have
a main program that invokes the compiler (the only C way of invoking
another program is system()), then invokes the code that copies it
into the emulated filesystem.

Gordon L. Burditt
May 13 '06 #7
On 2006-05-13, no*********@gma il.com <no*********@gm ail.com> wrote:

Ben C wrote:
On 2006-05-13, no*********@gma il.com <no*********@gm ail.com> wrote:
> Two questions.
>
> 1)Is there any way that I can read from an executable and then execute
> what I have read.
Interesting idea; it is possible on a machine on which code and data are
stored in the same memory (I think this is called a "von Neumann
machine")-- most modern desktop machines are like that. So I gave it a
go.

First I made a file called blah.c with this in it:

int add(a, b)
{
return a + b;
}

and compiled it to blah.o. [this would be called blah.obj on some
systems].

Then I disassembled blah.o and found the actual code of "add" was 11
bytes long and started at offset 0x34L:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 45 0c mov 0xc(%ebp),%eax
6: 03 45 08 add 0x8(%ebp),%eax
9: 5d pop %ebp
a: c3 ret

So then I just loaded the code directly into an array in another
program, cast the buffer to the proper function type (casting char * to
to a function type I'm pretty sure is "undefined behaviour", but I
thought it might work anyway), and called it:

#include <stdio.h>
#include <stdlib.h>

typedef int (*add_t)(int, int);

add_t load_add(void)
{
FILE *fp = fopen("./blah.o", "rb");
char *code = malloc(11);

fseek(fp, 0x34L, SEEK_SET);
fread(code, 1, 11, fp);

fclose(fp);

return (add_t) code;
}

int main(void)
{
add_t add = load_add();
int result = add(4, 5);

printf("%d\n", result);

return 0;
}


[snip]
As for the linking...I'm not exactly sure how to go about that, but it
apears that you have done am I correct? I'm not sure how I could write
a program that would link .o files makeing them executables, however,
scince I don't really care what the operating system that my mini-OS is
running on has to say about much of anything I'm not sure if it would
make much of a differance. Or would it?
I'm not intirely sure I know what I'm getting myself into here :p.
No nor am I :)
Anyway if you could explain what you ment by the mini-OS linking and
also about if it would be nesisary to have .exe files at all it would
be much apreciated by me.


To see what I mean by "linking", try the example I posted, but add this
to blah.c:

int muladd(int a, int b, int c)
{
return add(a, b * c);
}

Now try to "call" muladd from the main program by loading it into
memory... You will need to load both add and muladd, and somehow arrange
for the call to add from muladd to jump to the right place. This is
linking, basically-- setting up the path from one function to another.

The thing about exe files is they contain a lot of information specific
to the OS about how to load them into memory and run them. Doing
anything with them except just running them in the way the OS expects is
likely to be more complex than working with .obj files. If your mini-OS
wants to just run them then it's really just a shell rather than a
mini-OS.

The best thing is to try some things out and you will realize what's
needed and work out what's going on. Compile the .obj files you're
loading with minimal compiler optimizations to keep them as simple as
possible, and investigate disassembler programs; I would recommend GNU
objdump.
May 14 '06 #8


Ben C wrote:
On 2006-05-13, no*********@gma il.com <no*********@gm ail.com> wrote:

Ben C wrote:
On 2006-05-13, no*********@gma il.com <no*********@gm ail.com> wrote:
> Two questions.
>
> 1)Is there any way that I can read from an executable and then execute
> what I have read.

Interesting idea; it is possible on a machine on which code and data are
stored in the same memory (I think this is called a "von Neumann
machine")-- most modern desktop machines are like that. So I gave it a
go.

First I made a file called blah.c with this in it:

int add(a, b)
{
return a + b;
}

and compiled it to blah.o. [this would be called blah.obj on some
systems].

Then I disassembled blah.o and found the actual code of "add" was 11
bytes long and started at offset 0x34L:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 45 0c mov 0xc(%ebp),%eax
6: 03 45 08 add 0x8(%ebp),%eax
9: 5d pop %ebp
a: c3 ret

So then I just loaded the code directly into an array in another
program, cast the buffer to the proper function type (casting char * to
to a function type I'm pretty sure is "undefined behaviour", but I
thought it might work anyway), and called it:

#include <stdio.h>
#include <stdlib.h>

typedef int (*add_t)(int, int);

add_t load_add(void)
{
FILE *fp = fopen("./blah.o", "rb");
char *code = malloc(11);

fseek(fp, 0x34L, SEEK_SET);
fread(code, 1, 11, fp);

fclose(fp);

return (add_t) code;
}

int main(void)
{
add_t add = load_add();
int result = add(4, 5);

printf("%d\n", result);

return 0;
}


[snip]
As for the linking...I'm not exactly sure how to go about that, but it
apears that you have done am I correct? I'm not sure how I could write
a program that would link .o files makeing them executables, however,
scince I don't really care what the operating system that my mini-OS is
running on has to say about much of anything I'm not sure if it would
make much of a differance. Or would it?
I'm not intirely sure I know what I'm getting myself into here :p.


No nor am I :)
Anyway if you could explain what you ment by the mini-OS linking and
also about if it would be nesisary to have .exe files at all it would
be much apreciated by me.


To see what I mean by "linking", try the example I posted, but add this
to blah.c:

int muladd(int a, int b, int c)
{
return add(a, b * c);
}

Now try to "call" muladd from the main program by loading it into
memory... You will need to load both add and muladd, and somehow arrange
for the call to add from muladd to jump to the right place. This is
linking, basically-- setting up the path from one function to another.

The thing about exe files is they contain a lot of information specific
to the OS about how to load them into memory and run them. Doing
anything with them except just running them in the way the OS expects is
likely to be more complex than working with .obj files. If your mini-OS
wants to just run them then it's really just a shell rather than a
mini-OS.

The best thing is to try some things out and you will realize what's
needed and work out what's going on. Compile the .obj files you're
loading with minimal compiler optimizations to keep them as simple as
possible, and investigate disassembler programs; I would recommend GNU
objdump.


And I really thought that I was getting somewere too. Okay one
question and it sounds simple enough but here goes: How in the world do
I set it up to that muladd would jump to the right place? Also, would
doing things in this mannor require things that could not be automated?
I was really hoping that there would be some way to execute things
that were in the psudo-filesystem without actually doing manual work.
Thanks.
Nori

May 14 '06 #9
>> 1)Is there any way that I can read from an executable and then execute
what I have read.
Interesting idea; it is possible on a machine on which code and data are
stored in the same memory (I think this is called a "von Neumann
machine")-- most modern desktop machines are like that. So I gave it a
go.


It's not necessary for code and data to be in the same memory, or for
the target machine and the host machine to have instruction sets anything
like each other. All you need is a program running on the host that
emulates the target machine. All of the state of the target machine
(RAM, ROM, registers, etc.) is in the data space of the host machine.
There are lots of emulators for various machines, especially those for
CPUs often used in embedded controllers.
Then I disassembled blah.o and found the actual code of "add" was 11
bytes long and started at offset 0x34L:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 45 0c mov 0xc(%ebp),%eax
6: 03 45 08 add 0x8(%ebp),%eax
9: 5d pop %ebp
a: c3 ret


Note that if you move code from one address to another, the code
might change (particularly for (conditional) branch instructions
that aren't PC-relative, or if the code uses local data). This is
often called "relocation ". An object (as opposed to executable)
file has information needed to locate code anywhere. Executable
files might not (particularly if the OS uses virtual memory and
always loads executables at a fixed address).

Gordon L. Burditt
May 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2908
by: Sharp Tool | last post by:
Hi I have downloaded a program that is written in C++ that I would like to compile into a Win XP executable. The program contains a Makefile written for Unix/linux/cygwin. I would like to modify it, if neccessary, so that I can compile it using MS Visual C++ 6.0. Here is the code for the Makefile: //START
2
1309
by: Ganesh Ramamurthy | last post by:
Hi Experts, I need to get the output of a command line program in my C# application. The command line program is an decryption program which gives the decrypted text of a file specified as the parameter. Thanking in advance Regards Ganesh
29
3968
by: tele-commuter | last post by:
Hi folks, I want to understand how exactly is an image(compiled c code and loaded into memory) stored in memory. What exactly is a linker script? I work with a lot of c code on a daily basis but I really don't understand :
34
2998
by: priyanka | last post by:
Hi, I was wondering if we could parse or do something in the executable( whose source language was C). How can I use some scripting language like perl/python to find out the information about the executable ? Is it possible ? Also, how does the compiler add inling to the program ? I know that whenever it sees"inline" in front of the procedure name, it inlines it. But if we give the -finline options, it inline all the procedures ? How
2
1774
by: joseandremorales | last post by:
HI everyone, Im hoping you can help me with this question. I have written an app in C# .NET to enumerate all currently running processes in a pocketpc, i choose a process and then enumerate all of it's associated threads. Im stuck on figuring out how I can read a specific thread's R15 (program counter) to find out the next instruction it is going to execute from within my c# program, any ideas on this would be paramount and i thank for...
7
24817
by: patrik.kahari | last post by:
Is there a c++ function similar to getcwd that does not give you the working directory but the directory or path of the currently running executable? I need this because im porting a game that uses relative paths to images. This works fine when the game is executed directly from the directory its in (as in ./game &). It does not work when its executed from another location (as in /root/games/game &) Regards Patrik
21
626
by: Naya | last post by:
Hello, everyone!!! Well, I have a situation here. I am trying to read this data from a file, but the wrong values keep spitting out at me. Here's what I mean: Program: int main() { ifstream inFile;
4
2345
by: ovatta | last post by:
Hi! I have meke a simple programs on c++ with visual studio. The program have a dll where I put my name e my personal details. I realize that anybody can take my executable file and with a simpli resource editor can be edit, delete or replace my details with other data. How can block this possibility? How can prevent the resource editing? It's possible? Thanks.
1
1494
by: Ashish Gupra | last post by:
Hi All, I want to create an application as 'Object Browser' in Visual Studio.Net. In which I just want to give it a Dll or PDB file, then it will show the information about references used by this dll and classes (with method, property and variables) in this dll. Please give me any suggestion to implement such application. Regrads, Ashish
0
9725
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10976
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10705
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9477
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7877
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5714
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5906
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4111
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.