4

It came up in another question: What are the programs and parts called by gcc (particularly when compiling C or C++) so that someone might engineer some scheme of intercepting and altering the flow for various custom coding purposes?

Community
  • 1
  • 1
wallyk
  • 56,922
  • 16
  • 83
  • 148

2 Answers2

3

The compiler binaries are the "compiler driver" (i.e. gcc), and the compiler itself which also doubles as a preprocessor (cc1 or cc1plus). It also invokes the assembler (as), and the linker (ld). Additionally there's a tool called collect2 that assists during the link process in some cases.

If you want to see what the intermediate states and invocations are then do this:

gcc -save-temps -v .....

If you want to see the compiler's optimization passes, then use these options:

gcc -fdump-tree-all -fdump-rtl-all ....

This produces (vaguely) human readable dumps of the internal state for debugging purposes. It's nothing you could save and reload into the compiler later, that's for sure, but it's helpful if you plan to modify the compiler's source, or write a GCC plugin.

ams
  • 24,923
  • 4
  • 54
  • 75
  • `-fdump-tree-all` and `-fdump-rtl-all` are not all passes. I also use `-fdump-ipa-all` in recent gcc. Also, some information is still disabled when "`-fdump-`*fomrat*`-all`" is used. To get full (and more verbose) available passes dump, add an extra `-all` to get: `-fdump-ipa-all-all -fdump-tree-all-all -fdump-rtl-all-all`. To get verbose assembler listing, use `-fverbose-asm`. To see actions of linker, use `-Wl,--verbose`. – osgx Feb 07 '12 at 16:19
  • There are also a lot of options shown, when `cc1` is started with --help (may be used to get list of options enabled by `-O3` or `-O2`), e.g. `/usr/libexec/gcc/i686-redhat-linux/4.6.1/cc1 --help` – osgx Feb 07 '12 at 16:29
  • And then there's `-fdump-{tree,rtl}-all-details` for even more info. The list is all but endless. – ams Feb 07 '12 at 16:29
  • Are you sure, that -all-details is more verbose than -all-all? As I see, the -all-all is more verbose. – osgx Feb 07 '12 at 17:01
  • 2
    Yeal all-all includes "details", now I look closer. – ams Feb 07 '12 at 17:48
1

Observe exactly what programs are called:

gcc -v main.c

The exact steps are determined by a spec file with format: https://gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/Spec-Files.html

View the default (hard-coded in GCC):

gcc -dumpspecs

Run your own spec file after the default one:

gcc -specs=file
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • But the most important is the optimization passes (internal to `cc1` or `cc1plus`, etc...). You could use [MELT](http://gcc-melt.org/) to inspect them (or be notified about them, or add a new pass, or remove an existing one, etc....) – Basile Starynkevitch Jun 25 '15 at 16:21
  • @BasileStarynkevitch agreed, I left out the internal passes because ams had mentioned `-fdump` and focused only on the invoked programs. But studying internals is what brought me here in the first place (much more interesting!), and I'll definitely have a look at MELT ;-) – Ciro Santilli OurBigBook.com Jun 25 '15 at 16:24