GCC commits and patches

My GCC commits and patches

avr-gcc explorer

Run avr-gcc from your browser and compare generated code across versions

Why did the compiler do that - the one with VRP and integer size

Here’s a simple program - it right shifts a signed int by 9 bits, truncates it to an unsigned char, and returns 1 or 0 after comparing the result with 0x74. $ cat test.c extern int e; int main() { unsigned char result = e >> 9; // result = 0x74; if ((int)result != 0x74) return 1; return 0; } Let’s compile this for the avr target, choosing to optimize for size.

Thin LTO and compile time errors

GCC has had link time optimization (LTO) for quite a while now. Instead of generating just assembly code, it streams intermediate representation (IR) for the translation unit to the object file. At link time, when you provide all the object files necessary to link into the ELF, the compiler gets to see IR from all the translation units together, and this lets it perform optimizations across translation units. All you have to do is add -flto to the compiler and linker invocations and you’re done.

GCC's command line option handling and arbitrary values

Say you want to add your own command line option to the gcc driver. It’s pretty easy to do it as long as it is a number or a bitmask - just add an entry to your target’s opt file, and gcc’s option handling mechanism takes cares of it all. For example, this is how the avr target’s mn-flash option is described in avr.opt mn-flash= Target RejectNegative Joined Var(avr_n_flash) UInteger Init(-1) Set the number of 64 KiB flash segments.

Making use of objcopy's --update-section for fun and profit

The binutils project’s objcopy utility gained an –update-section feature with the 2.26 release. This flag lets you insert the contents of any file into an already existing section in the ELF. You can make use of this flag to do a couple of things that are currently tricky to get right - placing data computed from the contents of the ELF (say the SHA1 hash of flash contents) into a binary, and creating a combined bootloader/application image from two separate binary files.

Debugging a debugger - gdb crash on Windows XP

If you’re one of the crazy guys building gdb for Windows using the 4.6.1 version of the mingw gcc compiler, here’s something to watch out for. gdb will crash on any error (even a mistyped command), but only on Windows XP. Turns out that gdb uses a setjmp/longjmp based error handling mechanism (see gdb/exceptions.c), and any error causes gdb to execute a longjmp. In Windows land, longjmp is implemented in msvcrt.

LTO and the linker command line

If you’re using GCC’s LTO feature, make sure you repeat the compiler flags when linking. So, if you compile code with -Os -ffunction-sections, make sure you pass the same flags to the gcc driver when linking. This is needed because LTO effectively recompiles by streaming in IR from the object files and regenerating code. Only this time it knows about the code in all the object files that’s going to be part of the final executable, so it can perform a bunch of optimizations it previously couldn’t.