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 14 1663 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
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.
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.
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
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
>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
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.
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
>> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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 :
|
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
|
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...
| |
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
|
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;
|
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.
|
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
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |