目錄

韌體工作 C 語言快速複習

C 語言流程

Preprocessor,所有用 # 開頭的,如同以下:

  • Include Expansion
  • Macro Expansion
  • Conditional Compilation
  • Define: 記得要用 L 當你需要 long 型態的 variable,

常常會用 #error 看看有什麼變數有沒有定義。

再來開始編譯:

  • Lexical Analysis
  • Parsing 建立 AST
  • Semantic Analysis
  • Optimization
  • Generate IR (GCC 的 GIMPLE or LLVM 的 LLVM IR)
  • Generate Assembly

組譯 Assembler 產生 ELF Relocatable Object,包含

  • .text
  • .data
  • .bss
  • Symbol Table

用 Symbol Table Relocation,讓 Linker 連接。

  • Symbol Resolution 告訴像是 printf 在哪個 .so 檔案
  • Relocation 真的把函式 printf 定義為真的 address 位置

最後產生 ELF Executable。

執行的時候會有動態 Linker,使用 GOT/PLT。

  1. Kernel 載入看 ELF Program Header
  2. Linker 看 ELF Section Header

Inline Function

大部分函式情況,Inline Func 比較好,Debug 還是可以看到。

只有一些情況還可以用 Macro,像是字串相關的操作

  • Conditional Compilation
  • Stringification #define STR(x) #x
  • Token Pasting #define CONCAT(a,b) a##b
  • offsetof

你所不知道的 C 語言:前置處理器應用篇

左右規則

【譯】C語言「右左規則」 - 西灣筆記

Static/Const/Volatile

Volatile: 可能會隨時受到硬體變動

Re-entrant 函式

Operator

因為當表達式中存在singed與unsinged型態的時候,所有的運算元都會自動轉換為無符號類型(unsigned)。

字串

  • str[] 是對 stack allocate, *str = “a” 是 read-only data

Reference