Log in

View Full Version : Compiling code in memory


nathan
May 11th, 2006, 05:39
Hi all,

I was wondering if it is possible to make a compiler call (e.g. a syscall) and to compile code stored in memory and not in a file.
In practice I have a C code generator which will generate code which needs to be compiled through a syscall to gcc or else and I would like to avoid to store it in a temp file. Is this easily manageable ?

Thnx,

nathan

Silver
May 11th, 2006, 05:55
Can you explain your requirements a bit better? How a compiler takes input is a function of the compiler itself. If you have C code stored in memory I assume you have an app that generates this code on the fly. You'll need to bodge some compiler code into your app that takes its input from the var(s) that store the C code, rather than from disk.

Vague answer for a vague question

LLXX
May 11th, 2006, 06:12
Create two named pipes, one for sending to the gcc and one for receiving the output. The gcc can then read from and write to the named pipes, on the other end of which are two threads of your program, one to read from the named pipe and write to the memory, the other to write to the named pipe from memory.

OHPen
May 11th, 2006, 07:43
This seems to be rather crazy, don't you think ?

disavowed
May 11th, 2006, 09:06
What are you going to be using this for? Why can't you use a temp file?

Extremist
May 11th, 2006, 13:28
Hook CreateFileA(), ReadFile(), etc.

laola
May 11th, 2006, 14:21
If you are using gcc anyway, just modify its source to write to a buffer instead of a file That's why I love open-source At the same time you don't need the syscall anymore, just call your gcc directly.

nathan
May 12th, 2006, 01:57
First of all, thnx for all your replies. Here is some background:

I have developed a set of API which will allows to automatically create dedicated C programs with very specific functionalities (real app doesn't matter). I need to compile the generated code at run-time but I would like to avoid leaving (even temporary) files around which contains the actual code. It may not be the best way but this was my first though on how to do it (i.e., compiling from memory).

Thnx,

nathan

disavowed
May 12th, 2006, 09:43
Quote:
[Originally Posted by nathan]but I would like to avoid leaving (even temporary) files around

So don't leave them around. Create them, use them, then delete them. Where's the problem?

naides
May 12th, 2006, 10:01
Quote:
[Originally Posted by disavowed]So don't leave them around. Create them, use them, then delete them. Where's the problem?


Wild guess?

if you freeze the process, for instance using a debugger, in the time lapse between the files were created, are being compiled, but not yet deleted, you can make spare copies of Nathan supper dupper secret source files and study them to your content.

I do not even remember the names, but several protectors worked that way. . . the protection key code was contained in evanescent dll files with funky names like 3xcde443rtfg.dll

Which brings me to a question @ Nathan:

I am assuming you have a reason to keep the contents of those source files "secret" right?

having them in memory at least while they are being compiled would not stop a clever enough H4x0r from dumping them right there, from the RAM into a file.

So,What is there to gain with compiling from memory??

LLXX
May 12th, 2006, 17:39
Quote:
[Originally Posted by nathan]First of all, thnx for all your replies. Here is some background:

I have developed a set of API which will allows to automatically create dedicated C programs with very specific functionalities (real app doesn't matter). I need to compile the generated code at run-time but I would like to avoid leaving (even temporary) files around which contains the actual code. It may not be the best way but this was my first though on how to do it (i.e., compiling from memory).

Thnx,

nathan
Are you trying to produce a completely standalone IDE and C compiler that can run on a diskless workstation?

0xf001
May 12th, 2006, 18:46
hi,

gcc and its tools like the linker all generate temp files afaik. you can use gcc -pipe to use pipes rather than temp files. when using -pipe i figured compiling a .c source: it doesnt use a temp file for the tmp.s assembly source file, but still a tmp.o for the object file.

its a question if gcc + ld can work w/o tmp files at all i think ...

regards, 0xf001

LLXX
May 13th, 2006, 00:42
Maybe a more compact C compiler like TinyCC (http://tinycc.org) would suit your needs better?

nathan
May 15th, 2006, 05:31
Here are some more comments:

In general, it is not so critical that the so called "supper dupper source code" stays hidden, but it would be a "nice to have" feature. So I just wanted to stimulate some discussion in this forum and learn a little bit more about compiling from memory only (if possible at all).

naides: has got the point and, yes, I agree you could easily dump from memory (I haven't though about tackling this one, yet).

disavowed: naides has answered to your question.

LLXX: no the target is not a compiler for diskless workstations (but, hey , nice idea!) ... just working on automating some tasks within an EDA design flow.

0xf001: I'll try your suggestion and see what's happen. In general, your final statement is what I'm looking for ...

Thnx,

nathan

Aimless
May 15th, 2006, 09:13
Are you who I think you are?



Have Phun

nathan
May 15th, 2006, 13:23
I don't know ... PM me and you'l find out :-)

nathan