Compile C++ code into assembly and then de-assemble

1.9k views Asked by At

Does anyone know how convert C++ code to assembly code and then do the reverse? The forward way is very easy:

g++ -S

I want to analyze the output and see if it has been compiled correctly (Just for curiosity now, but it can have some applications). However, my knowledge of assembly is very limited and the output is hard to understand (This is especially true if I use optimizations (-O) or compile with debug info (-g) ).

Is there a de-assembler for C++ (GCC) to produce C++ code? If not, is there any intermediate representation that I can compile C++ code into and then back from it?

There seems to be some ways for converting C++ to C here. Does GCC have anything for this?

4

There are 4 answers

2
Jerry Coffin On BEST ANSWER

De-compiling assembly language back to C++ is possible (e.g., with HexRays), within some constraints -- primarily that although the C++ you get out will reflect the basic algorithms correctly, it probably won't look much like the original source code (though C++ name mangling does help maintain something closer to the original than you usually get with many other languages).

0
MSalters On

The latter question ("is there any intermediate representation that I can compile C++ code into and then back from it?") sounds like the AST produced by CLang.

0
Basile Starynkevitch On

Perhaps you might be interested in learning more about internal representations used by GCC, in particular GIMPLE (and Tree-s). If you want to take advantage of GCC numerous processing around GIMPLE, you should consider writing a GCC plugin or a GCC MELT extension (MELT is a high-level domain specific language to easily extend GCC).

But all the middle-end internal representations of the C++ compilers I know about are quite far from the C++ source code, because the C++ front-end has already done a lot of work, and there is no easy way to go back to some useful C++.

faithful and complete decompilation is in practice nearly impossible, because the assembly code generated by a compiler has lost some knowledge from the original source code.

0
jli On

Use objdump -d to disassemble a compiled object. Other than that you can't get much more information back out of it (and definitely not the original source). I'd trust the compiler if I were you.