Spring 2016

gcc notes

Browse the gcc man pages, i.e. ...
On my laptop : $ gcc -dumpversion # Aug 19 2010 4.2.1 $ gcc -dumpmachine i686-apple-darwin10 $ gcc -print-search-dirs install: /usr/lib/gcc/i686-apple-darwin10/4.2.1/ programs: =/usr/libexec/gcc/i686-apple-darwin10/4.2.1/:/usr/libexec/gcc/i686-apple-darwin10/4.2.1/: /usr/libexec/gcc/i686-apple-darwin10/:/usr/lib/gcc/i686-apple-darwin10/4.2.1/: /usr/lib/gcc/i686-apple-darwin10/:/usr/libexec/gcc/i686-apple-darwin10/4.2.1/: /usr/libexec/gcc/i686-apple-darwin10/:/usr/lib/gcc/i686-apple-darwin10/4.2.1/: /usr/lib/gcc/i686-apple-darwin10/: /usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../../i686-apple-darwin10/bin/i686-apple-darwin10/4.2.1/: /usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../../i686-apple-darwin10/bin/ libraries: =/lib/i686-apple-darwin10/4.2.1/x86_64/:/lib/x86_64/:/usr/lib/i686-apple-darwin10/4.2.1/x86_64/: /usr/lib/x86_64/:/opt/local/lib/i686-apple-darwin10/4.2.1/x86_64/:/opt/local/lib/x86_64/: /usr/lib/gcc/i686-apple-darwin10/4.2.1/x86_64/:/usr/lib/gcc/i686-apple-darwin10/4.2.1/x86_64/: /usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../../i686-apple-darwin10/lib/i686-apple-darwin10/4.2.1/x86_64/: /usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../../i686-apple-darwin10/lib/x86_64/: /usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../i686-apple-darwin10/4.2.1/x86_64/: /usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../x86_64/:/lib/i686-apple-darwin10/4.2.1/:/lib/: /usr/lib/i686-apple-darwin10/4.2.1/:/usr/lib/:/opt/local/lib/i686-apple-darwin10/4.2.1/: /opt/local/lib/:/usr/lib/gcc/i686-apple-darwin10/4.2.1/:/usr/lib/gcc/i686-apple-darwin10/4.2.1/: /usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../../i686-apple-darwin10/lib/i686-apple-darwin10/4.2.1/: /usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../../i686-apple-darwin10/lib/: /usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../i686-apple-darwin10/4.2.1/: /usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../ Compilation steps : * preprocessing *.c => *.i C after preprocess (text) * compile *.i => *.s assembler (text) * assemble *.s => *.o object (binary * link *.o => executable gcc recognizes those extensions; given one of these, it'll start with that stop. To get it to stop at an intermediate step : -E stop at *.i preprocess only; send to standard output -S stop at *.s produce assembler source (see -dA -dp -dP below) -c stop at *.o compile; don't link For any of those : -o file.extension put the output in named file To see all of the intermediates (.i, .s, .o) - save-temps Debugging : -g include debugging info ; native os format -ggdb include debugging info for gdb -g<level> default level is 2; higher for more info -p generate code for "prof" profiling -pg generate code for "gprof" profiling -fmemreport print some memory allocation stats -dA annotate assembler output with misc debug info -dp annotate assember output with pattern/alternative remarks -dP dump RTL (register transfer level) in assembler output as comment; google "rtl gcc" Machine (and what sort of assembler) specs -m32 what we want to look at IA32 (what the textbook describes) -m64 x86-64, -mtune=i686 close to -m32, with some stuff for newer cpus -march=name gazillions of options -mcpu=name ditto Verbose -v print the intermediate commands -### same, but don't do anything. Optimization -O0 none; what we want for looking at assembler -O2 what I usually use -Os optimize for size The *.i file includes "linemarkers, i.e. lines of the form # 94 "/usr/include/sys/_types.h" 3 4 which say that what follows came from line 94 of that file. The "3 4" are flags; google "gcc preprocessor" or see C allows declaring functions which take a variable number of arguments (for example printf); see or google "C variable length arguments". The <stdarg.h> library is used to manage this; see for example wikipedia's stdarg.h article. The assembler for gcc is "Gas" (the GNU Assembler); executable on my machine is "as"; type "man as" for the details. output is "x86_64 assembler", I guess $ as -v Apple Inc version cctools-773~33, GNU assembler version 1.38 See wikipedia's "x86 assembly language" and "x86 instruction listings" Dissassemble with "objdump" (or "gobjdump" as it installed itself on my laptop via binutils and "FSF development toolchain") Debug (after compiling with some of the debug options) with one of these; see tutorial at gdb command line gnu debuggger ddd (data display debugger; runs gdb etc in a GUI window) courses/ spring2016/systems/ code/ gcc_notes
last modified Monday January 25 2016 7:35 pm EST