开发者

Finely Control the output of GCC

For some reasons, I want to be able to fine tune the final executables generated for my C++ program - down to individual instructions. As I recall, GCC should generate some text assembler file as intermediate output before feeding it to the assembler for generating the final binary object file.

Is it possible for me to extract this assembler source, edit it programatically and feed this modified version to the final assembler for generating my custom object file? I am targeting the Windows platform.

Does MSVC++ support similar feature?

And if you wonder why, part of the reason is to prevent reverse engineering by including our custom polymorphic code generation logic. Another reason, is to create hidden identification within each copy distributed to customers in o开发者_如何转开发rder to deter anyone cracking our protection and distributing his cracked version.


Take a look at the -S flag:

g++ -S file.c -o output.s

Then make your fine tuning on output.s and compile the final executable with:

g++ output.s -o application


Yes. On Linux, try this:

$ cat hello.cc
#include <iostream>

int main() {
  std::cout << "Hello, world\n";
}

$ g++ -S hello.cc && sed -i 's/Hello, world/Goodbye, world/' hello.s && g++ -o goodbye hello.s && ./goodbye


Yes, gcc can produce assembler intermediate output at the compilation phase, via gcc -S. You can generate different assembler outputs with the various optimisation levels -On where n=0,1,2,3. If you do this, you'll notice -O3 in particular produces things that aren't, in your head, mapped directly to C code because gcc has implemented the code optimally.

MSVC can do the same thing via cl.exe /Fafilename.asm. The msdn documentation for this is here.

Note that if you want comparable-ish syntax you probably want gcc -masm=intel -S on the gcc end. Note also that there will be some differences between Microsoft's Macro assembler output and GNU AS's output. I'm not familiar with MASM but there are subtle differences between all assemblers (as well as the obvious AT&T vs Intel syntax).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜