CS31 Weekly Lab: Week 10

Week 10 lab topics:

  1. A reminder to look at the Week 9 lab page for help with topics related to lab 6:
    1. .h and .c files, scope, extern, static
    2. gdb for C programmers
    3. valgrind
    See: Week 9 lab topics and example code

  2. Phases of Compilation, Linking, and Loading:
    Very briefly today we will look at the phases of computation. In Tuesday's lecture we will discuss loading a bit more.

    First, grab some files into your cs31/inclass/10/ sub directory by running update31:

      update31
      cd ~cs31/inclass/10/
      ls
        Makefile  README  mylib.c  mylib.h  prog.c
    
    We are going to use this example code to illustrate the different stages of compiling a C program to an executable binary:
    1. The Preprocessor: expands #'s (#includes, #defines)
      To run just the preprocesor: gcc -E prog.c
    2. The Compiler: translates C code to relocatable object code (.o files). This is the C to IA32 translation step.
      To build a relocatable object file: gcc -c prog.c
    3. The Linker: takes multiple .o (and .a, and .so files), and generates an executable binary file. Most libraries are in /usr/lib/
      To run the linker: gcc -o myprog prog.o mylib.o

    After the linking phase there are still some undefined symbols from shared object files (.so). These are bound during the loading phase when the runtime linker runs.
    To see shared object dependencies: ldd prog

    Lets briefly look at the phases of computation using the example code. The README file has some directions for viewing the output from each phase and for comparing it to previous phase output.

    Loading takes place when you run your program. The program is loaded into RAM and the process' address space is created and initialized. Runtime linking may additionally take place if the executable file was linked with .so versions of libraries.

    If you are interested in learning more about linking and loading, they are covered in much more detail in Chapter 7. Also, in Compilers (CS75) linking is covered in more detail, and in OS (CS45) loading is covered in more detail. I also have some documentation about the C compilation cycle, linking and loading and about tools for examining the output from different stages in this process: C compilation cycle and tools