Introduction

You have used GCC for years. You know roughly what -O2 does. You have fought cannot find crt1.o at two in the morning. You have built a cross toolchain by copying somebody's shell script and changing the triple, and it worked, and you never found out why.

This book explains why, by following each decision GCC makes to the code that makes it. It covers which program the driver runs, where each header and library comes from, what a cross build waits for, and what cc1 does between your source and the assembler. Each chapter covers one mechanism, named in its title, and the chapters build on each other in order.

GCC's manuals document most options and procedures, and each chapter says where they do, with a link to the page. What the manuals do not show is the code. Every concrete claim in this book links to the line of GCC source that decides it, so a sentence you doubt is one click from the evidence. GCC's own comments often explain a decision better than prose could, and where they do, the book quotes them.

The book is also available to read offline, as an EPUB or a PDF. Both are built from this same source on every deploy.

Who this book is for

The book is for someone comfortable with C, ELF, readelf and the general shape of a linker. You do not need to have read a compiler book, because terms such as SSA are explained where a chapter first needs them.

Most chapters need only a compiler you already have. Nearly every mechanism has a command that shows it working, such as -###, -v, -print-search-dirs, -dumpspecs or -fdump-tree-all. Parts II and III need a GCC checkout or a build tree, and the line under each chapter title says what that chapter needs.

How a chapter is laid out

Every chapter has the same shape, so that you can find your way in any of them after reading one. The shape is also how the book separates what the manuals promise from what only the source shows.

  • The title names the mechanism. The line under it gives the GCC release and what you need to run the chapter's commands.
  • The opening describes a failure or a surprise you may have met, then states what the chapter shows. One sentence names the earlier chapters it builds on.
  • Each section opens with its point, then gives the evidence: a listing from GCC's source, whose caption links the exact lines, or a command to run.
  • "Three things to carry away" closes the body. Some chapters add "Things that surprise people".
  • "Documentation coverage" names the manual pages that document the mechanism, then lists what no manual documents. That list is the part of the book you will not find anywhere else.
  • "Next" says what the following chapter takes up.

Each part opens with a short page that lists its chapters, what each one needs, and the terms the part relies on.

Every GCC source link points at the release tag releases/gcc-15.2.0 on the read-only mirror at github.com/gcc-mirror/gcc, and every line number was read from that tag, not from a working tree. Line numbers move with nearly every patch, while the code around them rarely changes, so in another release a cited line is usually a few lines away. Where a mechanism itself changed between major releases, the sentence that states it says so. A reader on an older compiler is then warned where they read the claim, not in an appendix they may never reach.

A few chapters cite the projects GCC works with. Their links point at release tags too:

ProjectReleaseCited in
glibc2.42Chapters 3.1, 3.3 and 3.5
newlib4.5.0Chapters 3.4 and 3.6
binutils2.45Chapter 4.4
DejaGnu1.6.3Chapters 3.7 and 6.1

Commands, output and untested recipes

A command block shows output only when that output was captured, and a caption under it says where. When a block shows no output, the text after it says what the command prints and which source line decides that. Some recipes build GCC, a C library or the manuals, and take hours to run. Each one that was not run end to end carries an "Untested recipe" note. The note says what was checked against the source and what was not, so that you know where to expect trouble before you start the build.

File names before GCC 12: .c, not .cc

GCC 12 renamed nearly every .c file in the compiler to .cc, in a single commit, 5c69acb3 (Martin Liska, 2022-01-14). So gcc.c became gcc.cc, gimplify.c became gimplify.cc, and aarch64.c became aarch64.cc. Blog posts, mailing-list threads and answers written before the rename use the old names, and so does a GCC 11 or older tree. In those, nearly every file name differs from this book's by one letter: add or drop the second c as needed.

The six parts, from using GCC to changing it

The parts go deeper as they go on, because each one moves further from the compiler you have installed. They cover using it, building it, what it ships, what happens inside it, and changing it. You can stop at the end of any part.

Part I, "The driver is not the compiler", is about the program you type. gcc compiles nothing. It decides which other programs to run and builds each one's command line. That one fact explains most of what looks arbitrary about GCC from outside. Part I is the longest part, because almost every path, prefix, library and header decision lives in the driver.

Part II covers configuring and building GCC itself. Part III covers bootstrapping a cross toolchain, where the compiler and the C library each need the other to exist first, and then testing what you built. Part IV covers the runtime libraries GCC ships: what libgcc is for, the unwinder, and the choice between static and shared runtimes. Part V goes inside cc1: the three intermediate representations, the pass manager, and how a machine description becomes an instruction. Part VI covers GCC as a project: writing tests, its conventions, and how a patch gets proposed.

Four distinctions the whole book relies on

Four distinctions recur in every part, and most published explanations of GCC get at least one of them wrong. Each one is a place where two mechanisms look like one from outside.

Library search paths. The driver never opens a library file. It emits -l and -L options and lets ld resolve them. A library search path, in GCC's sense, is a list of strings that the driver hands to the linker.

Link time and run time. A successful link says nothing about whether the program will start. At run time the dynamic loader, ld.so, finds shared libraries through DT_RUNPATH, ld.so.conf and LD_LIBRARY_PATH. The book stops at the file the linker writes, except in Chapter 4.4.

Header search and library search. The same prefix relocates both, which makes them look like one mechanism. Different code in different files implements them, with different sysroot rules.

Host programs and target files. Anything under libexec/gcc/ is a host program, which runs on your machine. Anything under <target>/lib/ is a target file, which never runs on your machine: it is only linked into what your compiler produces. Chapter 1.2 is mostly about keeping the two apart.