Including compiled C source file in array

Discussion in 'C Programming' started by sachin, Jun 5, 2008.

  1. sachin

    sachin Guest

    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
    sachin, Jun 5, 2008
    #1
    1. Advertising

  2. sachin

    santosh Guest

    sachin wrote:

    > 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.
    santosh, Jun 5, 2008
    #2
    1. Advertising

  3. In article <>,
    sachin <> wrote:

    >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
    --
    In the selection of the two characters immediately succeeding the numeral 9,
    consideration shall be given to their replacement by the graphics 10 and 11 to
    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
    Richard Tobin, Jun 5, 2008
    #3
  4. sachin

    Richard Guest

    (Richard Tobin) writes:

    > In article <>,
    > sachin <> wrote:
    >
    >>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


    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.
    Richard, Jun 5, 2008
    #4
  5. In article <g28kua$bag$>,
    Richard <> wrote:

    >>>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?


    >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
    --
    In the selection of the two characters immediately succeeding the numeral 9,
    consideration shall be given to their replacement by the graphics 10 and 11 to
    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
    Richard Tobin, Jun 5, 2008
    #5
  6. sachin

    sachin Guest

    On Jun 5, 5:25 pm, (Richard Tobin) wrote:
    > In article <g28kua$>,
    >
    > Richard <> wrote:
    > >>>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?

    > >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
    > --
    > In the selection of the two characters immediately succeeding the numeral 9,
    > consideration shall be given to their replacement by the graphics 10 and 11 to
    > facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)


    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
    sachin, Jun 5, 2008
    #6
  7. In article <g28k6g$2opa$>,
    Richard Tobin <> wrote:
    >In article <>,
    >sachin <> wrote:
    >
    >>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?


    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.
    Kenny McCormack, Jun 5, 2008
    #7
  8. sachin <> wrote:
    > On Jun 5, 5:25 pm, (Richard Tobin) wrote:
    > > In article <g28kua$>,
    > >
    > > Richard <> wrote:
    > > >>>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?
    > > >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.

    >
    > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jun 5, 2008
    #8
  9. On 5 Jun 2008 at 12:44, sachin wrote:
    > 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.
    Antoninus Twink, Jun 5, 2008
    #9
  10. sachin

    Flash Gordon Guest

    Antoninus Twink wrote, On 05/06/08 17:18:
    > On 5 Jun 2008 at 12:44, sachin wrote:
    >> 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.


    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.
    --
    Flash Gordon
    Flash Gordon, Jun 5, 2008
    #10
  11. sachin

    jaysome Guest

    On Thu, 5 Jun 2008 12:51:56 +0000 (UTC),
    (Kenny McCormack) wrote:

    >In article <g28k6g$2opa$>,
    >Richard Tobin <> wrote:
    >>In article <>,
    >>sachin <> wrote:
    >>
    >>>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?

    >
    >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.

    --
    jaysome
    jaysome, Jun 6, 2008
    #11
  12. sachin

    Dan Guest

    "Flash Gordon" <> wrote in message
    news:-gordon.me.uk...
    > Antoninus Twink wrote, On 05/06/08 17:18:
    >> On 5 Jun 2008 at 12:44, sachin wrote:
    >>> 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.

    >
    > 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
    Dan, Jun 6, 2008
    #12
  13. sachin

    sachin Guest

    On Jun 6, 2:49 pm, "Dan" <> wrote:
    > "Flash Gordon" <> wrote in message
    >
    > news:-gordon.me.uk...
    >
    > > Antoninus Twink wrote, On 05/06/08 17:18:
    > >> On 5 Jun 2008 at 12:44, sachin wrote:
    > >>> 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.

    >
    > > 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.
    sachin, Jun 9, 2008
    #13
  14. sachin <> wrote:
    > On Jun 6, 2:49 pm, "Dan" <> wrote:
    > > "Flash Gordon" <> wrote in message
    > > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jun 9, 2008
    #14
  15. sachin

    Flash Gordon Guest

    Jens Thoms Toerring wrote, On 09/06/08 16:00:
    > sachin <> wrote:
    >> On Jun 6, 2:49 pm, "Dan" <> wrote:
    >>> "Flash Gordon" <> wrote in message
    >>> 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.
    --
    Flash Gordon
    Flash Gordon, Jun 9, 2008
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jd
    Replies:
    0
    Views:
    420
  2. Klaus Schneider
    Replies:
    1
    Views:
    528
    Rolf Magnus
    Dec 2, 2004
  3. Marc Jeambrun

    first C source file to be compiled

    Marc Jeambrun, Dec 1, 2006, in forum: C Programming
    Replies:
    6
    Views:
    275
    Stephen Sprunk
    Dec 1, 2006
  4. lander
    Replies:
    5
    Views:
    579
    bruce barker
    Mar 5, 2008
  5. Ray Muforosky
    Replies:
    3
    Views:
    102
    Anno Siegel
    Apr 22, 2005
Loading...

Share This Page