Including compiled C source file in array

S

sachin

Hi,

Is it possible to do something like this:

unsigned char arr[] = {
#include "cFile.c"
}

I need that C source file cFile.c to compile and its binary output to
include in array.

How we can achieve it? Any other trick to do this?

Thanks,
Sachin
 
S

santosh

sachin said:
Hi,

Is it possible to do something like this:

unsigned char arr[] = {
#include "cFile.c"
}

I need that C source file cFile.c to compile and its binary output to
include in array.

How we can achieve it? Any other trick to do this?

You can compile cFile.c and copy it's bytes, (as hexadecimal, decimal or
octal text. All binary editors can show this for you), to your array
declaration.

Alternatively there might be compiler specific extensions for this,
though I doubt it. You might also consider the possibility of loading
the binary file at runtime with fread.
 
R

Richard Tobin

sachin said:
I need that C source file cFile.c to compile and its binary output to
include in array.

What do you expect to be able to do with it?

-- Richard
 
R

Richard

What do you expect to be able to do with it?

-- Richard

I would expect he has something which produces code based on dynamic
variables and wants to have variable records e.g accept user tweaks,
regenerate record structures, recompile record handling SW, run record
handling SW.
 
R

Richard Tobin

What do you expect to be able to do with it?
[/QUOTE]
I would expect he has something which produces code based on dynamic
variables and wants to have variable records e.g accept user tweaks,
regenerate record structures, recompile record handling SW, run record
handling SW.

Possibly, but unless he tells us we can't give him much help. For
most uses he'd be wisest to to use system-dependent functions that
(for example) access the symbol table, and reading the compiled file
into an array may well not be the right way to do that.

-- Richard
 
S

sachin

Possibly, but unless he tells us we can't give him much help. For
most uses he'd be wisest to to use system-dependent functions that
(for example) access the symbol table, and reading the compiled file
into an array may well not be the right way to do that.

-- Richard

Hi,

I have one C source file which after compiling I need to copy at
specific location.
Until system comes up I don't have access to that memory location. So
I want to keep
that code in executable form with the OS image. When system comes up
will copy that
executable code to designated space.

Thanks,
Sachin
 
K

Kenny McCormack

What do you expect to be able to do with it?

Why, to smash the stack, of course.

It's perfectly obvious that all of these queries about needing to
include (seemingly) random sequences of bytes in code are about
"cracking/hacking/freeking/whatever-you-want-to-call-it" - let's not get
into that argument...

Not necessarily for evil purposes, mind you. It's worth the exercise
just to learn what all the fuss is about.
 
J

Jens Thoms Toerring

sachin said:
I have one C source file which after compiling I need to copy at
specific location.
Until system comes up I don't have access to that memory location. So
I want to keep
that code in executable form with the OS image. When system comes up
will copy that
executable code to designated space.

You can't simply include a binary file (the C source will be
of no use anyway). Just write a little utility that opens the
file you want to insert as data at that place, reads the data
from the file in as unsigned chars and writes them out in a
form like this:

unsigned char array[ ] = {
0x01, 0x3A, 0xA6, 0x12, 0x17, 0xC3, 0x7D, 0x5F,
.....
};

Save that to a file (e.g. data.h). Then include the data.h
file at exactly the place where you want the array to be
defined. Lets hope that the binary file isn't too long to
be stored in an array.
Regards, Jens
 
A

Antoninus Twink

I have one C source file which after compiling I need to copy at
specific location.

If you're writing a virus, the man's way is to write the shellcode by
hand in assembly, not use a compiled C file.
 
F

Flash Gordon

Antoninus Twink wrote, On 05/06/08 17:18:
If you're writing a virus, the man's way is to write the shellcode by
hand in assembly, not use a compiled C file.

On one DSP I used to use there was a perfectly good and "standard"
reason for wanting to achieve what the OP wants to achieve. However, the
implementation provided special tricks in the linker to allow you to
achieve it which did not involved doing what the OP asked about.

The OP needs to ask about the real problem rather than what he thinks
the solution is in a group dedicated to his implementation.
 
J

jaysome

Why, to smash the stack, of course.

It's perfectly obvious that all of these queries about needing to
include (seemingly) random sequences of bytes in code are about
"cracking/hacking/freeking/whatever-you-want-to-call-it" - let's not get
into that argument...

Not necessarily for evil purposes, mind you. It's worth the exercise
just to learn what all the fuss is about.

Not necessarily, indeed.

Suppose you found some C source code that computes a 32-bit CRC used
in a common communications protocol. You figured out a way to
implement the CRC calculation in a better way. And in order to verify
your code, you need some "random sequences of bytes" to test against
the C source code you found against your implementation.

Given that the C source code you found is correct, what better way to
verify your implementation than to test it on the binary data stored
in files by recursively reading these files in a given directory.

Of course the concept of a directory is not something specified by the
C Standard, but if there are compiler-specific functions that deal
with directories (as there are in Windows and Linux and Mac OS X),
then using such functions are a benefit rather than a hindrance for
the purposes of verification.

The test program is simple: recurs the directory and read each file in
binary mode and compute the CRC of the file using the C source code
you found and the C source code of your implementation. If the results
differ, output the path of the file. Then use your favorite debugger
to figure out the problem.

Sometimes you have to put on your software engineering hat in favor of
your C Standard hat. In response to that, some here may say "woe is
you for doing that"; perhaps there really is more to it than that and
perhaps they should really rethink their ways.
 
D

Dan

Flash Gordon said:
Antoninus Twink wrote, On 05/06/08 17:18:

On one DSP I used to use there was a perfectly good and "standard" reason
for wanting to achieve what the OP wants to achieve. However, the
implementation provided special tricks in the linker to allow you to
achieve it which did not involved doing what the OP asked about.

Indeed I do this thing nearly every day. I have a C program that has the
compiled binaries of other C programs (including itself), so it can send
itself and other programs to other processors. On embedded systems with no
file structure this is easiest way because you don't have to mess around
with linker/address problems. The compiler has the ability to turn any file
into an array of chars by going char foo[] = __PRAGMA_FILE("whatever.file");
Getting the output into the same program is a bit more tricky, as you will
get stuck in a compliaton loop :-D
 
S

sachin

Antoninus Twink wrote, On 05/06/08 17:18:
On one DSP I used to use there was a perfectly good and "standard" reason
for wanting to achieve what the OP wants to achieve. However, the
implementation provided special tricks in the linker to allow you to
achieve it which did not involved doing what the OP asked about.

Indeed I do this thing nearly every day. I have a C program that has the
compiled binaries of other C programs (including itself), so it can send
itself and other programs to other processors. On embedded systems with no
file structure this is easiest way because you don't have to mess around
with linker/address problems. The compiler has the ability to turn any file
into an array of chars by going char foo[] = __PRAGMA_FILE("whatever.file");
Getting the output into the same program is a bit more tricky, as you will
get stuck in a compliaton loop :-D

How __PRAGMA_FILE works. How to use it in our source code?
Can you please explain this. I don't know much about pragma.
 
J

Jens Thoms Toerring

sachin said:
Indeed I do this thing nearly every day. I have a C program that has the
compiled binaries of other C programs (including itself), so it can send
itself and other programs to other processors. On embedded systems with no
file structure this is easiest way because you don't have to mess around
with linker/address problems. The compiler has the ability to turn any file
into an array of chars by going char foo[] = __PRAGMA_FILE("whatever.file");
Getting the output into the same program is a bit more tricky, as you will
get stuck in a compliaton loop :-D
How __PRAGMA_FILE works. How to use it in our source code?
Can you please explain this. I don't know much about pragma.

It's something specific to the compiler Flash's using on his
system, it's nothing every compiler has to support. Obviously,
it converts the contents of a file at compile time into an
expression that can be used to initialize a char array. But
unless you know you have the exact same compiler as Flash you
will have to study your compiler documentation. If you're very
lucky you may have something similar, perhaps going by a dif-
ferent name.
Regards, Jens
 
F

Flash Gordon

Jens Thoms Toerring wrote, On 09/06/08 16:00:
sachin said:
Indeed I do this thing nearly every day. I have a C program that has the
compiled binaries of other C programs (including itself), so it can send
itself and other programs to other processors. On embedded systems with no
file structure this is easiest way because you don't have to mess around
with linker/address problems. The compiler has the ability to turn any file
into an array of chars by going char foo[] = __PRAGMA_FILE("whatever.file");
Getting the output into the same program is a bit more tricky, as you will
get stuck in a compliaton loop :-D
How __PRAGMA_FILE works. How to use it in our source code?
Can you please explain this. I don't know much about pragma.

It's something specific to the compiler Flash's using on his
system, it's nothing every compiler has to support. Obviously,

No, it is specific to the compiler Dan used. The implementation I used
worked in an entirely different way to achieve the same effect.
it converts the contents of a file at compile time into an
expression that can be used to initialize a char array. But
unless you know you have the exact same compiler as Flash you
will have to study your compiler documentation. If you're very
lucky you may have something similar, perhaps going by a dif-
ferent name.

Or it may be completely different but allowing the same effect to be
achieved. Anyone needing to do this needs to ask on a group dedicated to
their specific implementation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top