Including compiled C source file in array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sachin

    Including compiled C source file in array

    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

  • santosh

    #2
    Re: Including compiled C source file in array

    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.

    Comment

    • Richard Tobin

      #3
      Re: Including compiled C source file in array

      In article <b647a47d-5e08-46f4-ba2e-f0a4df255907@u1 2g2000prd.googl egroups.com>,
      sachin <sbillore@redif fmail.comwrote:
      >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)

      Comment

      • Richard

        #4
        Re: Including compiled C source file in array

        richard@cogsci. ed.ac.uk (Richard Tobin) writes:
        In article <b647a47d-5e08-46f4-ba2e-f0a4df255907@u1 2g2000prd.googl egroups.com>,
        sachin <sbillore@redif fmail.comwrote:
        >
        >>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.

        Comment

        • Richard Tobin

          #5
          Re: Including compiled C source file in array

          In article <g28kua$bag$2@r egistered.motza rella.org>,
          Richard <rgrdev@gmail.c omwrote:
          >>>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)

          Comment

          • sachin

            #6
            Re: Including compiled C source file in array

            On Jun 5, 5:25 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
            In article <g28kua$ba...@r egistered.motza rella.org>,
            >
            Richard <rgr...@gmail.c omwrote:
            >>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

            Comment

            • Kenny McCormack

              #7
              Re: Including compiled C source file in array

              In article <g28k6g$2opa$1@ pc-news.cogsci.ed. ac.uk>,
              Richard Tobin <richard@cogsci .ed.ac.ukwrote:
              >In article <b647a47d-5e08-46f4-ba2e-f0a4df255907@u1 2g2000prd.googl egroups.com>,
              >sachin <sbillore@redif fmail.comwrote:
              >
              >>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.

              Comment

              • Jens Thoms Toerring

                #8
                Re: Including compiled C source file in array

                sachin <sbillore@redif fmail.comwrote:
                On Jun 5, 5:25 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                In article <g28kua$ba...@r egistered.motza rella.org>,

                Richard <rgr...@gmail.c omwrote:
                >>>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 ___ jt@toerring.de
                \______________ ____________ http://toerring.de

                Comment

                • Antoninus Twink

                  #9
                  Re: Including compiled C source file in array

                  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.

                  Comment

                  • Flash Gordon

                    #10
                    Re: Including compiled C source file in array

                    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

                    Comment

                    • Dan

                      #11
                      Re: Including compiled C source file in array


                      "Flash Gordon" <spam@flash-gordon.me.ukwro te in message
                      news:c0tlh5xvbs .ln2@news.flash-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


                      Comment

                      • sachin

                        #12
                        Re: Including compiled C source file in array

                        On Jun 6, 2:49 pm, "Dan" <vo...@sometwhe r.worldwrote:
                        "Flash Gordon" <s...@flash-gordon.me.ukwro te in message
                        >
                        news:c0tlh5xvbs .ln2@news.flash-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.

                        Comment

                        • Jens Thoms Toerring

                          #13
                          Re: Including compiled C source file in array

                          sachin <sbillore@redif fmail.comwrote:
                          On Jun 6, 2:49 pm, "Dan" <vo...@sometwhe r.worldwrote:
                          "Flash Gordon" <s...@flash-gordon.me.ukwro te 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 ___ jt@toerring.de
                          \______________ ____________ http://toerring.de

                          Comment

                          • Flash Gordon

                            #14
                            Re: Including compiled C source file in array

                            Jens Thoms Toerring wrote, On 09/06/08 16:00:
                            sachin <sbillore@redif fmail.comwrote:
                            >On Jun 6, 2:49 pm, "Dan" <vo...@sometwhe r.worldwrote:
                            >>"Flash Gordon" <s...@flash-gordon.me.ukwro te 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

                            Comment

                            Working...