Chapter 31 - Compilers and Assemblers #
Common Flags and When to Use them #
These flags are not universal, instead they’re from GCC. GCC and Clang are the big two compilers you’ll see a lot and these flags should be mostly the same between the two. Of course, there are many, many other compilers, but I’m going to start with the common stuff
-o (Output) #
Note, this is a lowercase ‘o’
You should’ve seen this already, I’m mentioning it mostly to give a brief review of what a flag is, it’s basically just another term for an argument to a program where the program is run as
program-name argument1 argument2 ... but, usually flags are prefixed by
in the case of
-o it just means, use the next argument as the output name, so using
gcc input.c -o myprogram gives you an executable named myprogram instead of just input.
Just, read this: https://interrupt.memfault.com/blog/best-and-worst-gcc-clang-compiler-flags - It’s a lot more in depth than anything I could write here. It’s focused on Embedded development, but should be broadly applicable.
-S (aSSembly) #
-v (Verbose) #
Note, this is an uppercase ‘O’
And now we can get to fun stuff. -O is generally used in levels, here with no space, so there’s
-Ofast are for speed, and
-Os is for size of the binary. (or
-Oz for clang)
-Ofastcan break your code. This is rather unlikely though.
Each of these will take longer to compile but be more aggressive about optimizing the assembly code that is generated from your input. For example, let’s look at the code
This is a very stupid multiply function. Effectively, it’s multiplying two numbers by making a matrix of size m by n, and then counting the number of spots in that matrix instead of just multiplying the two numbers. I’ve embedded a GodBolt page below with this code on the left, and it’ll show the assembly output on the right. At the top you should be able to see a box to type compiler options, go ahead and put in
-O. You should see a huge long mess of assembly that used multiple
imul (multiply) instructions be reduced to 3 lines of assembly with only a single multiply. The compiler has figured out that what we were doing was a stupid multiply and optimized it for us.
Here there’s no difference between
O3, but with more complex code there often is. Just keep in mind that this is a trade off, there is the upfront cost of a longer compile time.
-march, -mtune, -flto, -msse4.2, -mavx, -mavx2 #
Stripping (Not like that, perv) #
Understanding compilers and common compiler flags #
Making a compiler and assembler, DIY language eso-lang style
Making an interpreted language #
Making a compiled language #
Garbage Collection #
Continued Reading #
At this point I highly recommend ‘The Art of Exploitation’ by Jon Erickson