News for hackers. Stuff that matters.
Re:A native code to C/C++ decompiler by jordigh on Wednesday October 08, 2014 @08:29AM (#8426520)
C and C++ are different languages. I only saw C examples. How would you even decompile to C++? The only C++ information you have in the object code is the mangled names. How do you use that to get C++ code?
Re:A native code to C/C++ decompiler by _wmd on Wednesday October 08, 2014 @08:53AM (#8426607)
And vtables, rtti data, systematic sequences of ops for invoking base classes, intrinsic/library functions emitted by the compiler in specific situations, exception handling tables, ...
Re:A native code to C/C++ decompiler by jordigh on Wednesday October 08, 2014 @04:43PM (#8429348)
But none of those is something only a C++ compiler would specifically do, right? How would you distinguish a vtable emitted by a C++ compiler from one hand-rolled in C? I suppose you could just offer a reasonable guess. I wonder if decompiled GTK+ code would end up turning into C++.
Re:A native code to C/C++ decompiler by mieko on Thursday October 09, 2014 @06:45AM (#8431588)
Most C++ platform ABIs are pretty trivial to recognize. A tool like this could distinguish a C++ binary by looking at its initialization/housekeeping sections, static constructors, __cxa_atexit, exception handling tables, linked libraries, etc.

For example, the Itanium C++ ABI (http://mentorembedded.github.io/cxx-abi/, perhaps Itanium's only real legacy) adopted by Linux/ELF and other platforms, leaves a huge amount of fingerprints on a binary. It'd take a very conscious effort, including hand-crafting linker scripts, to generate a C binary that a tool would incorrectly think was C++.

Re:A native code to C/C++ decompiler by pjmlp on Wednesday October 08, 2014 @08:58AM (#8426624)
As some other decompilers do, by having a knowledge pool of specific compilers.

Back in the 90's there was one targeted specifically to executables produced by Borland compilers.

Re:A native code to C/C++ decompiler by userbinator on Wednesday October 08, 2014 @10:09AM (#8426919)
The first C++ compiler (Cfront) compiled from C++ to C.

Once you get some C code that does things with structures and function pointers like a C++ compiler would do, I think it's not impossible to turn those back into classes if you can recognise the patterns that a C++ compiler uses to compile C++ constructs like classes, virtual functions, etc.

Re:A native code to C/C++ decompiler by jacquesm on Wednesday October 08, 2014 @12:25PM (#8427761)
It's actually a lot harder than C. There is a roughly 1:1 correspondence between C and assembly. There is no such 1:1 correspondence between assembly and C++.
Re:A native code to C/C++ decompiler by Hemospectrum on Wednesday October 08, 2014 @01:22PM (#8428106)
> There is a roughly 1:1 correspondence between C and assembly.

I'm afraid that hasn't been true for a number of years. Turn on the optimizer and the resulting assembly can be totally unrecognizable as a translation of what you actually wrote. The compiler takes all kinds of steps both to eliminate redundant operations (via CSE, loop unrolling, etc.) and to take advantage of the weird quirks of modern CPU architectures, like out-of-order execution.

Re:A native code to C/C++ decompiler by dwyer on Wednesday October 08, 2014 @04:11PM (#8429184)
Just yesterday I was debugging a piece of code I was writing. I started with a printf where I suspected a segfault was occurring.

  printf(...);
  for (...) {...}
The printf never executed. So I fired up gdb and confirmed the segfault was happening where I thought it was, but the for loop was being initialized before the printf. Even with debugging on and optimization off. Could be a bug in clang (I didn't think to try it with gcc), but even so it shows that the way things should work in theory don't necessarily dictate the way they do work in practice.
Re:A native code to C/C++ decompiler by userbinator on Wednesday October 08, 2014 @05:45PM (#8429638)
There is a roughly 1:1 correspondence between C++ and C, so it's just one more step to go from C to C++. Just off the top of my head...

    class data fields -> structure members
    non-static methods (ctors/dtors included) -> functions with an extra 'this' pointer parameter
    inheritance -> extended structures with matching prefices
    virtual functions -> structure with function pointers
    operator overloading -> methods with special names
    default arguments -> automatically inserted by compiler
    function overloading -> types encoded in name of function
    templates -> whatever code they generate
    exceptions -> special library functions are used, e.g. _CxxThrowException()
Some of those don't decompile so well (e.g. template-generated code, although a refactoring tool might be of help), and things like operators and overloaded functions are probably more of a stylistic choice than a difference in the generated code, but for the most part it doesn't look impossible to get some C++ features to show up in decompiled code. (And given that many of the C++-level optimisations revolve around removing unnecessary code, they could make the C to C++ step even easier.)