Is there a way to decompile a C++ .obj object file into its original source code?
Nope.
You can decompile an obj file into some sort of source code, but not the original.
Example:
void main(void) {printf("hello world
");}
and
char s={‘h’,‘e’,‘l’,‘l’,‘o’,’ ‘,‘w’,‘o’,‘r’,‘l’,‘d’,’
'};
void main (void) {
printf(s);
}
both will compile to make the exact same obj file. There is no way for a decompiler to figure out what you did.
Any comments in the code will also be lost in either case.
Hmm, alright… so is there a way to decompile it to any source code, whether it be the original or not?
Yes. I’ve seen it done on an HP-UX box, but can’t remember the command. Keep looking.
You’ll also lose variable/function names, type definitions, and some control structures. The decompiler can’t really tell a while loop from a for loop.
The best decompilers are the ones designed to work with the output of a specific compiler, because they’re aware of the particular idioms that compiler uses. In the general case, it’s much harder to generate source code that’s anywhere near readable.
Actually, no strictly-conforming C++ compiler will compile either of your examples at all, since main() must return int. But even if we overlook this fact, I should note that the second example defines a global variable while the first example uses a constant for the output string. I suppose a smart compiler might be able to optimize away the variable given that it’s never used for any other purpose, but your average naive compiler is going to leave it in and thus produce different code.
Just make sure that if you’re decompiling software developed by someone else you have their permission. A lot of EULA’s forbid reverse engineering, so if you decompile something and distribute or talk about the resulting source you could get into trouble.
(The solution, try free/open source software)
UnuMondo
It’s debatable as to whether such restrictions carry any legal force. Prevailing legal thought is that if you’ve bought (or licenced) something, you can do whatever you want to it in the privacy of your own home. (Emphasis here on privacy – obviously if you intend to publicize the results of your meddlings, you may run into copyright issues.) I believe even the US’s notorious DMCA explicitly provides for the right to reverse-engineer. Preventing someone from reverse-engineering software makes about as much sense as preventing the mechanically curious from opening up an alarm clock to see how it works, or preventing a musician from fiddling with an equalizer and various filters in order to isolate an instrument track on a CD.
psychonaut, if you actually believe that, the current state of US copyright law will make you weep.
The solution here is to either find an open-source implementation of whatever the compiled code does or to write it yourself. Disassembly is a painfully tedious project, even if you can reliably seperate code from data and know enough assembly to make the results meaningful.
Which sort of eliminates the need try to get back to C++ code from the object files. Not that this is a bad thing. It just might not be what Civil Defense needs to do.
What if, say, his company had a piece of software written and received the source code to it. Now, some years later, they need to modify and recompile the program for some reason. Ooops! The source for foo.obj has gotten lost (file corruption, incomplete back up, whatever.) Now what? Especially if you determine that what you need to change is in foo.cpp?
Anyway, take a look here for more info:
http://www.cs.uregina.ca/links/class-info/210/C++FAQ/compiler-dependencies.html#[33.4]
Minor nitpick, but that version of the C++ FAQ (from my old university, no less) is horribly out of date, and, knowing how anal the author is about such things, is probably being mirrored without permission. You can find more up-to-date information at the FAQ’s main location.
Right you are. There is an updated version. It still amounts to “If it is some one else code, SOL and bad boy for even trying. If it is your own code, SOL and make backups the next time.”