-1

I would like to use GCC to compile my C/C++ code and output low-level C code. Is it possible to have GCC use C as an output target?

The intended purpose is to obsfucate a piece of code in such a way that it is difficult to reverse engineer. I believe if GCC reduces input source code down to an IR and then spits out the functional equivalent of the IR in C, then the resulting piece of code would still be compilable but not readable.

I also found this thread: https://stackoverflow.com/questions/1025494/obfuscating-c-c-code

Apparently some commercial obsfucation tools use this technique internally as part of their solution.

Community
  • 1
  • 1
Guy
  • 167
  • 9
  • 5
    What do you mean by "low-level C code"? And what is "assembly-level C"? – juanchopanza Jul 22 '14 at 18:48
  • 1
    Try this: http://stackoverflow.com/questions/737257/how-to-convert-c-code-to-c – mclaassen Jul 22 '14 at 18:50
  • Well, a long time ago you would use `cfront` to compile your C++ into C... – crashmstr Jul 22 '14 at 18:51
  • The `-E` option will output preprocessed source code to standard output; this will include expanded macros and the contents of any included files. This will not emit a "lower-level" C code from what you have already written, though. – John Bode Jul 22 '14 at 18:58
  • Could whoever up-voted this question explain what it is asking for? – juanchopanza Jul 22 '14 at 19:03
  • What exactly do you mean by low level of abstraction? I know you can make settings for optimization (speed, size) but abstraction I have not heard of yet (in that context) – ryyker Jul 22 '14 at 19:08
  • @JohnBode - put your comment in an answer, and it will qualify for an automatic up-vote. Lots of precedence for that here. – ryyker Jul 22 '14 at 19:09
  • @mclaassen - The changes you made to the post change the whole meaning, and I suspect possibly do _not_ reflect what it is the OP is actually after. – ryyker Jul 22 '14 at 19:13
  • I was going off of this comment: "Yes, I would like to emit C code and not assembly, but at a low level of abstraction. " – mclaassen Jul 22 '14 at 19:16
  • @mclaassen - fair enough, and I agree OPs comments seem to indicate one thing, and Post another, but in general edits made by other than the OP are better if they _clarify_ the intended message (formatting, spelling, etc) without _modifying_ the core issue. BTW, I have no real issue with your edit, its just a nit with me that only OP should change course :) – ryyker Jul 22 '14 at 19:23
  • You could output assembly and then use a [decompiler](http://stackoverflow.com/questions/193896/whats-a-good-c-decompiler) to get C code – M.M Jul 23 '14 at 03:23
  • 1
    [Here](http://pastebin.com/xR5RF6zx) is the result of decompiling Hello, world using `printf`. (you don't even want to see the iostream version). – M.M Jul 23 '14 at 03:50
  • @MattMcNabb Holy crap that is godawful stuff. – mclaassen Jul 24 '14 at 00:35
  • @mclaassen it shows you how much compilers do behind the scenes I guess – M.M Jul 24 '14 at 00:37

1 Answers1

0

Yes you can. From here:

Use the -S (note: capital S) switch to GCC, and it will emit the assembly code to a file with a .s extension. For example, the following command:

gcc -O2 -S -c foo.c

will leave the generated assembly code on the file foo.s.

EDIT:-

Probably I am not sure but are looking for this:

# create assembler code:
c++ -S -fverbose-asm -g -O2 abc.cc -o abc.s
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Isn't he asking for it to emit C code, not assembly? – mclaassen Jul 22 '14 at 18:48
  • Yes, I would like to emit C code and not assembly, but at a low level of abstraction. – Guy Jul 22 '14 at 18:48
  • 3
    Nice. 3 up votes for an answer that doesn't answer the question. – mclaassen Jul 22 '14 at 18:53
  • 1
    @mclaassen - _C/C++ code to assembly-level C_ at least seems to imply this is what OP is looking for. I would have answered the same way (in fact I tried, but was beat to the punch) – ryyker Jul 22 '14 at 18:59
  • @Guy - What exactly do you mean by _low level of abstraction_? I know you can make settings for optimization (speed, size) but abstraction I have not heard of yet (in that context) – ryyker Jul 22 '14 at 19:02
  • My finger did twitch over the up-click button (nice clear concise answer), possibly propagating the rep of an answer that really does not answer the core issue. But since OP is not responding very often, no harm, no foul, right? However, from what _has_ been said, I think OP wants to generate code that has been fully devolved from using macros or other composites into C discretes. – ryyker Jul 22 '14 at 19:33