Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using GCC to output commented & annotated intermediate files

Tags:

c++

linux

gcc

g++

Is it possible to convince GCC to emit an intermediate file which shows:

  1. comments
  2. original source
  3. expanded macro definitions
  4. optimizations applied by compiler
  5. resulting C or C++ code which will be turned in to assembly code?

I'd rather see intermediate C/C++ instead of assembler, but I can use just assembler too if it's sufficiently annotated.

I am trying to reverse engineer a library composed almost entirely of macros in order to extend it. I'd also like to see the effects of optimization, in order to give the compiler more opportunities to do more optimization. (In other words, to see where my previous attempts have been ineffective)

like image 581
John Dibling Avatar asked Oct 04 '13 20:10

John Dibling


1 Answers

GCC applies optimizations not in the C++-code directly but in some internal language-independant format (called GIMPLE) which cannot be reverted into C++ code that easily.

Depending on what you want, you can either

  • just expand macros: g++ -E

  • or look at an assembler output where you can see which line of C++ code maps to which assembler block:

    g++ -g ... && objdump -S output
    

    I don't recommend outputting assembler directly from gcc (with -S) as the generated annotations are almost useless.

like image 176
ipc Avatar answered Sep 23 '22 06:09

ipc