Program loading. loading 會將程式從硬碟上複製到到記憶體上, 好讓程式可以執行. 有時候loading只要做複製的動作, 但有時候需要做配置記憶體的動作或設定protection bits.
Relocation. Compilers和assemblers通常將每個編譯好的程式(object code)的記憶體位置設定從零開始. 但是只有很少的電腦可以讓程式從
零開始. 當有多個程式包含有許多副程式的時候, 所有副程式需要被配置在non-overlapping的位置. Relocation就是負責幫程式指定(assigning)
load addresses. 在很多系統中, relocation通常不只發生一次.
Symbol resolution. 當程式要從其他副程式建置的時候. 程式references到副程式通常是用symbols. 例如一個程式要使用sqrt的副程式.會直接寫sqrt這個symbols. 而linker會將紀錄location assigned to sqrt in the library. 然後patching caller's(主程式) object code.
雖然linking和loading有許多重複的地方. 但是通常還是將他們區分為. 做 program loading 的是loader, 而做symbol resolution的是linker. 兩者都能做relocation.
Two-Pass Linking
linker負責處理被compiler或assembler編譯過的object files. 對linker來說就是一個input file. 而每個input file 包含了a set of segments.
每個input file 也至少包含一個symbol table. 有些symbol table是exported的, 用來給其他檔案使用. 當一個linker執行的時候, 會先掃過
一次input files確定每個segments的大小, 和蒐集每個symbol的 definitions 和 references. 然後產生一個segment table列出所有在 input
file被定義的segments. 還有一個symbol table包含export 和import的symbols. 接下來linker會指定記憶體位置給symbols, 決定segments
位置的大小. 第二次pass, 使用第一次pass的資訊產生relocation code放到output file. 而output file 通常會有header information, relocated
segments, and symbol table information. 如果是dynamic linking,symbol table會包含run-time linker所需要的資訊.
Relocation and code modification是主要linker&loader的工作.
2007/09/26
Linking vs. Loading
Posted by
Zhuang, Shang-Yi
at
3:16 pm
Labels: Embedded related
