跳转至

Lecture 13 Running a Program – CALL

CALL
  • Compiling
  • Assembling
  • Linking
  • Loading

alt text

How do we run a program?

  • Translator
    • converts a program from the src language to an equivalent program in other language
    • translating / compiling to lower-level languages almost always means higher efficiency, higher performance
  • Interpreter
    • directly execute a program in the src language
    • C and RISC-V can also be interpreted
Translating / Interpreting Language

你可以在该网站数据库运维部分进行查询,我在那里写了常见的语言分类说明

alt text

Bash
1
2
3
4
5
6
7
8
# foo.c (High-Level Programming)
gcc -S -O2 foo.c
# foo.s (Assembly Language, including some pseudoinstructions)
gcc -c foo.s
# foo.o (Object Code, Machine Language) + (Info for linking and debugging)
gcc foo.o
# a.out
./a.out

PS: if you input gcc foo.c, it will automatically execute 3 steps sequently mentioned above.

Compiler

alt text

  • Output can include pseudoinstructions
  • Often generate directions for assembler

Assembler

alt text

Directives

  • Often generated from the compiler (previous stage)
  • Give directions to the assembler

Directives do not produce machine instructions! Rather, they inform how to build different parts of the object file

alt text

Producing Machine Code

alt text

alt text

Other References cannot be determined yet, hence we need two tables

alt text

alt text

alt text

Object File Format

alt text

Bonus: check out objdump foo.o

C
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// psudo_format of foo.o
Object File Header: 
    // "metadata"
    size and position of other pieces of the object file
Text Segment:
    // "content"
    machine code itself
Data Segment:
    // "static data"
    binary representation of static data in src file
Symble Table:
    // "Lists informing to other programs"
    Lists of files label, static data that can be referenced by other programs
Relocation Information:
    // "Lists of where to fix up"
    Lines of code to be fixed up later (by Linker)
Debugging Information:
    // ...

Linker

alt text

Linker Patches Together Multiple Object Modules

alt text

Reallocation

alt text

alt text

Static and Dynamic Linking

alt text

alt text

Linker

alt text

alt text