binary to C (gcc GNU/Linux compiled machine code)

Discussion in 'C Programming' started by catcalls, Jan 9, 2011.

  1. catcalls

    catcalls Guest

    Hi groupies,

    Any program for GNU out there that basically converts the machine code
    generated by GCC back to C?

    Tried searching the group, could not find anything.

    Basically, in a nutshell, I wrote a C program under GNU/Linux a few
    years ago using standard C libraries to boot. And compiled it, but
    lost the source code to the program. Now, all I have left is a Fedora
    5 binary!

    I want my source code back because it was during deep hacking on the
    code that I found the solution to the problem, and I do not think I
    could write the source again. Its impossible.

    I just need my original C code back.

    Any help?
    catcalls, Jan 9, 2011
    #1
    1. Advertising

  2. catcalls

    Ian Collins Guest

    On 01/ 9/11 05:08 PM, catcalls wrote:
    > Hi groupies,
    >
    > Any program for GNU out there that basically converts the machine code
    > generated by GCC back to C?


    No. There isn't a two way translation between executables and C.

    > I just need my original C code back.
    >
    > Any help?


    Backups!

    --
    Ian Collins
    Ian Collins, Jan 9, 2011
    #2
    1. Advertising

  3. catcalls

    catcalls Guest

    On Jan 9, 4:14 am, Ian Collins <> wrote:
    > On 01/ 9/11 05:08 PM, catcalls wrote:
    >
    > > Hi groupies,

    >
    > > Any program for GNU out there that basically converts the machine code
    > > generated by GCC back to C?

    >
    > No. There isn't a two way translation between executables and C.
    >
    > > I just need my original C code back.

    >
    > > Any help?

    >
    > Backups!
    >
    > --
    > Ian Collins


    Oh, I was taking regular backups to my website. But, shortly after
    finishing the code and releasing the binaries on-line, my computer
    crashed hard. It was a nightmare. Just thankful that I have working
    binaries after all these years.

    Btw...I also have a Windows binary if there is a program for Windows
    that converts machine code to C? It's standard C so I thought that it
    wouldn't be a problem converting back?
    catcalls, Jan 9, 2011
    #3
  4. catcalls

    catcalls Guest

    On Jan 9, 4:23 am, catcalls <> wrote:
    > On Jan 9, 4:14 am, Ian Collins <> wrote:
    >
    >
    >
    > > On 01/ 9/11 05:08 PM, catcalls wrote:

    >
    > > > Hi groupies,

    >
    > > > Any program for GNU out there that basically converts the machine code
    > > > generated by GCC back to C?

    >
    > > No. There isn't a two way translation between executables and C.

    >
    > > > I just need my original C code back.

    >
    > > > Any help?

    >
    > > Backups!

    >
    > > --
    > > Ian Collins

    >
    > Oh, I was taking regular backups to my website. But, shortly after
    > finishing the code and releasing the binaries on-line, my computer
    > crashed hard. It was a nightmare. Just thankful that I have working
    > binaries after all these years.
    >
    > Btw...I also have a Windows binary if there is a program for Windows
    > that converts machine code to C? It's standard C so I thought that it
    > wouldn't be a problem converting back?


    http://www.hex-rays.com/decompiler.shtml

    See, I found this program. But, I was wondering if there is a GNU
    version? For GCC compiled code?
    catcalls, Jan 9, 2011
    #4
  5. catcalls

    Seebs Guest

    On 2011-01-09, catcalls <> wrote:
    > Any program for GNU out there that basically converts the machine code
    > generated by GCC back to C?


    Short answer: Yes.
    Equally good short answer: No.

    > Tried searching the group, could not find anything.


    That's because gcc-specifically, it's not topical here. Generically,
    it's mostly not possible.

    > I want my source code back because it was during deep hacking on the
    > code that I found the solution to the problem, and I do not think I
    > could write the source again. Its impossible.


    I'd guess you could.

    > I just need my original C code back.


    If you didn't compile with full debugging symbols, you are probably
    screwed.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Jan 9, 2011
    #5
  6. catcalls

    Uno Guest

    On 1/8/2011 9:27 PM, catcalls wrote:

    > http://www.hex-rays.com/decompiler.shtml
    >
    > See, I found this program. But, I was wondering if there is a GNU
    > version? For GCC compiled code?


    If there is one, I'd like to see how it works on the average K&R2
    problem, as shown on the wiki.
    --
    Uno
    Uno, Jan 9, 2011
    #6
  7. catcalls

    Jorgen Grahn Guest

    On Sun, 2011-01-09, Ian Collins wrote:
    > On 01/ 9/11 05:08 PM, catcalls wrote:
    >> Hi groupies,
    >>
    >> Any program for GNU out there that basically converts the machine code
    >> generated by GCC back to C?

    >
    > No. There isn't a two way translation between executables and C.


    Well, there *is*, but the C code wouldn't look anything like what he
    originally wrote. It would look more like the source for an x86 (or
    whatever architecture he used) emulator.

    There might exist such a program ... but the audience would be very
    small and so would the benefits.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Jan 9, 2011
    #7
  8. catcalls <> writes:
    > Any program for GNU out there that basically converts the machine code
    > generated by GCC back to C?
    >
    > Tried searching the group, could not find anything.
    >
    > Basically, in a nutshell, I wrote a C program under GNU/Linux a few
    > years ago using standard C libraries to boot. And compiled it, but
    > lost the source code to the program. Now, all I have left is a Fedora
    > 5 binary!
    >
    > I want my source code back because it was during deep hacking on the
    > code that I found the solution to the problem, and I do not think I
    > could write the source again. Its impossible.
    >
    > I just need my original C code back.


    Too much information is lost during compilation for it to be
    possible, even theoretically, to get your original C code back from
    an executable. (Well, a compiler could maintain enough information
    in the generated executable to allow this, but in practice they
    don't.)

    For example, compilation (and linking) will almost certainly lose
    the names of any identifiers that aren't externally visible, and may
    lose the externally visible ones as well. A for loop and a while
    loop might result in identical machine code; there's no way to tell
    which one was originally written. And if the code was compiled
    with optimization, it's likely to be mangled beyond recognition.
    It's like reconstructing a raw egg from an omelette.

    It's certainly possible, given an executable, to generate a C
    program that has the same behavior, but it's not at all likely to
    be legible or maintainable.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 9, 2011
    #8
  9. catcalls

    Chris H Guest

    In message <
    ..com>, catcalls <> writes
    >Hi groupies,
    >
    >Any program for GNU out there that basically converts the machine code
    >generated by GCC back to C?
    >
    >Tried searching the group, could not find anything.
    >
    >Basically, in a nutshell, I wrote a C program under GNU/Linux a few
    >years ago using standard C libraries to boot. And compiled it, but
    >lost the source code to the program. Now, all I have left is a Fedora
    >5 binary!
    >
    >I want my source code back because it was during deep hacking on the
    >code that I found the solution to the problem, and I do not think I
    >could write the source again. Its impossible.
    >
    >I just need my original C code back.
    >
    >Any help?


    You don't say what the target MCU/CPU is or if it runs on an OS/RTOS

    There are tools that help automate the process but nothing that is fully
    automatic.

    Unless you had all the debug information compiled in you will have to
    manually name all the variables.

    Due to optimisation etc within the compiler and linker you will never
    get back to the original source code. Especially as GCC is not ISO-C

    I have tools that can do some of the work but it is only a semi
    automatic process. It is probably far better to start again.

    --
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    Chris H, Jan 9, 2011
    #9
  10. catcalls

    catcalls Guest

    On Jan 9, 9:54 am, Chris H <> wrote:
    > In message <
    > .com>, catcalls <> writes
    >
    >
    >
    > >Hi groupies,

    >
    > >Any program for GNU out there that basically converts the machine code
    > >generated by GCC back to C?

    >
    > >Tried searching the group, could not find anything.

    >
    > >Basically, in a nutshell, I wrote a C program under GNU/Linux a few
    > >years ago using standard C libraries to boot. And compiled it, but
    > >lost the source code to the program. Now, all I have left is a Fedora
    > >5 binary!

    >
    > >I want my source code back because it was during deep hacking on the
    > >code that I found the solution to the problem, and I do not think I
    > >could write the source again. Its impossible.

    >
    > >I just need my original C code back.

    >
    > >Any help?

    >
    > You don't say what the target MCU/CPU is or if it runs on an OS/RTOS
    >
    > There are tools that help automate the process but nothing that is fully
    > automatic.
    >
    > Unless you had all the debug information compiled in you will have to
    > manually name all the variables.
    >
    > Due to optimisation etc within the compiler and linker you will never
    > get back to  the original source code. Especially as GCC is not ISO-C
    >
    > I have tools that can do some of the work but it is only a semi
    > automatic process.  It is probably far better to start again.
    >
    > --
    > \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    > \/\/\/\/\ Chris Hills  Staffs  England     /\/\/\/\/
    > \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/


    Thanks for all the replies. I guess I should just be thankful that I
    have working binaries for Windows and GNU.

    At least I can use the programs as I intended them to be used ~ but ~
    for how long?
    catcalls, Jan 9, 2011
    #10
  11. On Jan 9, 4:08 am, catcalls <> wrote:


    > Any program for GNU out there that basically converts the machine code
    > generated by GCC back to C?


    sort of.

    > Tried searching the group, could not find anything.


    such programs are called decompilers. They don't work very well as too
    much information is discarded in the compilation process.

    > Basically, in a nutshell, I wrote a C program under GNU/Linux a few
    > years ago using standard C libraries to boot. And compiled it, but
    > lost the source code to the program. Now, all I have left is a Fedora
    > 5 binary!
    >
    > I want my source code back because it was during deep hacking on the
    > code that I found the solution to the problem, and I do not think I
    > could write the source again. Its impossible.


    its morepossible to reconstruct your program than it is to decompile
    it. Use the executable as a specification and write a program that
    does the same thing. What sort of "deep hacking" were you doing that
    is so hard to duplicate?



    > I just need my original C code back.
    >
    > Any help?
    Nick Keighley, Jan 9, 2011
    #11
  12. catcalls

    Geoff Guest

    On Sat, 8 Jan 2011 20:08:00 -0800 (PST), catcalls
    <> wrote:

    >Hi groupies,
    >
    >Any program for GNU out there that basically converts the machine code
    >generated by GCC back to C?
    >
    >Tried searching the group, could not find anything.
    >
    >Basically, in a nutshell, I wrote a C program under GNU/Linux a few
    >years ago using standard C libraries to boot. And compiled it, but
    >lost the source code to the program. Now, all I have left is a Fedora
    >5 binary!
    >
    >I want my source code back because it was during deep hacking on the
    >code that I found the solution to the problem, and I do not think I
    >could write the source again. Its impossible.
    >
    >I just need my original C code back.
    >
    >Any help?


    Start writing the application again.

    Decompiling, as others have stated, isn't possible. What you get is a
    very poor decompilation at best. Reverse engineering the binary is
    actually more difficult than the task you face of re-writing the
    application from memory. Who knows, you might even code it better.

    Faced with a binary, you first have to disassemble it, then in a
    step-wise series of iterations, identify variables and bits of code
    and eventually get to an assembly listing with enough human readable
    information that you can identify library calls and argument values
    such that you can write C functions based on the disassembled code.
    You not only have to know C, but you have to know assembler, the
    target environment and the insides of the compiler that generated the
    code.

    The effort of all this is expensive, time consuming and unwarranted
    except where the value of the code is extremely high or the size of
    the program is very small. Reversing is usually only possible for very
    small programs like worms and viruses where the analysis is needed to
    fix the vulnerabilities they exploit.
    Geoff, Jan 9, 2011
    #12
  13. On 09 Jan 2011 04:13, catcalls wrote:
    > At least I can use the programs as I intended them to be used ~ but ~
    > for how long?


    The Linux one will probably work for decades; the Windows one may well
    stop working (correctly or at all) with your next OS upgrade.

    In either case, though, the problem is rarely that the program's
    behavior changes; it's that your definition of "working" changes and,
    without the ability to change the program to keep up, you'll gradually
    find it less and less useful.

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
    Stephen Sprunk, Jan 9, 2011
    #13
  14. catcalls

    Eric Sosman Guest

    On 1/9/2011 10:24 AM, Stephen Sprunk wrote:
    > On 09 Jan 2011 04:13, catcalls wrote:
    >> At least I can use the programs as I intended them to be used ~ but ~
    >> for how long?

    >
    > The Linux one will probably work for decades; the Windows one may well
    > stop working (correctly or at all) with your next OS upgrade.


    *No* Linux program, not even one, has worked "for decades."

    (Hint: Torvalds *started* developing Linux in 1991, slightly
    less than "decadeS" ago.)

    > In either case, though, the problem is rarely that the program's
    > behavior changes;[...]


    True. Linux' threading model and libc implementation have been
    rock-stolid. (Right?)

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 9, 2011
    #14
  15. catcalls

    Seebs Guest

    On 2011-01-09, China Blue Ribbon <> wrote:
    > An executable doesn't know what compiler it came from. A decompiler should work
    > as well regardless of the source language or compiler.


    Not true. You can recognize idioms used by a particular compiler.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Jan 9, 2011
    #15
  16. catcalls

    Lew Pitcher Guest

    On January 9, 2011 12:27, in comp.lang.c, wrote:

    > On 2011-01-09, China Blue Ribbon <> wrote:
    >> An executable doesn't know what compiler it came from. A decompiler
    >> should work as well regardless of the source language or compiler.

    >
    > Not true. You can recognize idioms used by a particular compiler.


    modulo compiler-applied optimizations

    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
    Lew Pitcher, Jan 9, 2011
    #16
  17. catcalls

    ImpalerCore Guest

    On Jan 8, 11:08 pm, catcalls <> wrote:
    > Hi groupies,
    >
    > Any program for GNU out there that basically converts the machine code
    > generated by GCC back to C?
    >
    > Tried searching the group, could not find anything.
    >
    > Basically, in a nutshell, I wrote a C program under GNU/Linux a few
    > years ago using standard C libraries to boot. And compiled it, but
    > lost the source code to the program. Now, all I have left is a Fedora
    > 5 binary!
    >
    > I want my source code back because it was during deep hacking on the
    > code that I found the solution to the problem, and I do not think I
    > could write the source again. Its impossible.
    >
    > I just need my original C code back.
    >
    > Any help?


    If it was a mechanical hard drive crash, you may be able to recover
    something from the drive if you can find a company that can reseat the
    magnetic disk in a new enclosure and try to probe the directory and
    file structures for your source code files. It's somewhat costly and
    not guaranteed to work, but it's an option for the desperate.

    Consider it an expensive and important lesson in backup management. I
    backup my work daily on my own personal USB stick, an external hard
    drive, a network backup, which is then sent to tape each night (though
    IT has been periodically undependable) to backup my work. I have had
    two of those options go down at the same time, but not all four.

    If you're willing to invest in some public or private online source
    repository, it can be another option to backup your code online.

    Good luck.
    John D.
    ImpalerCore, Jan 10, 2011
    #17
  18. catcalls

    robin Guest

    "catcalls" <> wrote in message
    news:...
    | Hi groupies,
    |
    | Any program for GNU out there that basically converts the machine code
    | generated by GCC back to C?
    |
    | Tried searching the group, could not find anything.
    |
    | Basically, in a nutshell, I wrote a C program under GNU/Linux a few
    | years ago using standard C libraries to boot. And compiled it, but
    | lost the source code to the program. Now, all I have left is a Fedora
    | 5 binary!
    |
    | I want my source code back because it was during deep hacking on the
    | code that I found the solution to the problem, and I do not think I
    | could write the source again. Its impossible.
    |
    | I just need my original C code back.

    The best way is via necktop computer.
    robin, Jan 11, 2011
    #18
  19. catcalls

    Angel Guest

    On 2011-01-09, catcalls <> wrote:
    >
    > I want my source code back because it was during deep hacking on the
    > code that I found the solution to the problem, and I do not think I
    > could write the source again. Its impossible.
    >
    > I just need my original C code back.


    To do this you need a tool called a decompiler. However, since a lot of
    metadata gets lost during compilation (even more so if the binary is
    stripped aftterwards), results may leave a lot to be desired.

    I may be mistaken, but I don't think there are any GNU tools that do
    this. The closest thing would be gdb, the GNU debugger, which can at
    least tell you what your program is doing.

    You may be better of just rewriting your program. I'm sure people here
    can help you if you encounter a problem you can't solve by yourself.
    Maybe you should describe the actual problem here?


    --
    The natural state of a spammer's website is a smoking crater.
    Angel, Jan 11, 2011
    #19
    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. Peter

    1 day gnu, whole life gnu?

    Peter, Jan 10, 2005, in forum: Java
    Replies:
    3
    Views:
    338
    John C. Bollinger
    Jan 10, 2005
  2. Peter
    Replies:
    17
    Views:
    603
    Chris Smith
    Jan 13, 2005
  3. Klaus Schneider
    Replies:
    1
    Views:
    540
    Rolf Magnus
    Dec 2, 2004
  4. Durduran
    Replies:
    10
    Views:
    536
    Durduran
    Jul 30, 2007
  5. Joe Young
    Replies:
    2
    Views:
    1,240
    Keith Thompson
    Aug 9, 2011
Loading...

Share This Page