約 23 分鐘3,150 字2,593 個單字
英文版本 Everything You Never Wanted To Know About DLLs.
最近因為一些原因,我需要調研動態連結在Windows平臺上的實現細節。這篇文章主要是總結我在這個問題上所學到的知識,用於我將來的回顧和參考,但同時我也希望這篇文章對其他人所有幫助,因為我將要總結的這些內容,你可能需要東找西找才能找到。
廢話不多說,讓我們開始這趟旅程吧:
匯出和匯入
Windows執行檔載入器(Windows executable loader)負責在執行程式前完成所有動態載入和符號解析工作。連結器會分別計算出每一個可執行映象(可執行映象是一個DLL或者一個EXE檔案)匯出和匯入了哪些函式,這個過程是通過檢查可執行映象的.edata段和.idata段來進行的。
關於.edata段和.idata段的詳細資訊,在PE/COFF specification這篇文件中有詳細說明。
.edata段
.edata段記錄了可執行映象匯出的符號(是的,EXE也可以匯出符號)。主要包括:
- 匯出地址表(export address table):一個長度為N的陣列,儲存了匯出的函式/變數的地址(相對於可執行映象起始地址的相關地址)。這張表的索引稱之為序號(ordinals)。
- 匯出名稱地址表(export name pointer table):一個長度為M的並行陣列(譯者記:其實就是兩個大小一樣的陣列,一個儲存key,一個儲存對應的value),儲存的是符號地址到匯出名稱的對映。這個平行陣列是按照匯出名稱的字典序排序的,從而允許進行對一個給定的匯出符號名稱進行二分查詢。
- 匯出序號表(export ordinal table):也是一個長度為M的並行陣列,儲存了序號到對應的匯出名稱的對映,其中匯出名稱對應的是匯出名稱地址表中的鍵key。
(作為通過名稱匯入符號這一做法的另一種替代方法,也可以通過指定序號來匯入一個符號。通過序號來匯入符號的做法在執行時會稍微快一些,因為這種情況下動態連結器(dynamic linker)不需要進行查詢(譯者記:根據名稱在匯出名稱地址表中找到名稱對應的地址)。此外,如果匯出符號的DLL並沒有給某個匯出項分配名稱,那麼通過序號來匯入符號是唯一的可行之路。)
那麼.edata段最初是怎樣被建立的呢?主要有兩種方法:
-
最常見的一種情況,在編譯生成一些目標檔案(object files)時會建立.edata段。這些目標檔案對應的原始碼中,定義了一些帶__declspec(dllimport)修飾符的函式或者變數。於是編譯器就會產生一個包含了這些匯出項的.edata段。
-
另一種比較少見的情況,開發人員會寫一個.def檔案,指定那些函式需要被匯出。將這個.def檔案提供給dll tool --output-exp,就能產生一個匯出檔案(export file)。匯出檔案是一個僅包含.edata段的目標檔案,匯出了在.def檔案中宣告的符號(匯出了一些未解析的引用,通常連結器會填寫這些引用到一個實際的地址)。程式設計師在將這些目標檔案連結成DLL的時候,必須對這些匯出庫(export library)進行命名。
對於以上兩種情況,連結器在連結時都會從所有的目標(objects)中收集.edata段,用於給整個可執行映象檔案建立一個.edata段。最後一種可能的方式是.edata段可以被連結器自身所建立,不需要將.edata段放入到任何目標檔案中:
-
連結器可以選擇在連結時匯出目標檔案中的所有符號。例如,這是GNU ld的預設行為(也可以通過--export-all-symbols顯式地指定這種行為)。在這種情況下,由連結器來產生.edata段。(GNU ld也支援在命令列中指定一個.def檔案,然後產生的.edata段就只會匯出這個.def檔案中宣告的符號)。
.idata段
.idata段記錄了可執行映象匯入的符號資訊。包括:
對於匯入符號涉及到的每一個可執行映象:
- 可執行映象的檔名。被動態連結器用於在磁碟上查詢該檔案。
- 匯入符號查詢表(import lookup table):一個長度為N的陣列,每一項要麼是一個序號,要麼是一個指向匯入名稱字串的指標。
- 匯入符號地址表(import address table):一個長度為N的指標陣列。動態連結器會用從匯入符號查詢表中對應順序的符號的地址來填寫該陣列。
.idata段中的條目以如下方式被建立:
-
最常見的一種情況,這些條目來自目標檔案中的匯入庫(import library)。可以對你希望匯出符號的DLL或者我們之前討論過的.def檔案使用dlltool工具來建立匯入庫。和匯出庫一樣,使用者必須在連結這些匯入庫時指定名稱。
-
或者,有一些連結器(像GNU ld)也可以讓你在連結時直接指定DLL檔案。對於你需要從這些DLL檔案中匯入的符號,連結器會自動產生相應的.idata段條目。
注意,跟匯出符號不一樣,__declspec(dllimport)修飾符並不會導致產生相應的.idata段。
比起第一次出現,匯入庫有點更復雜了。Windows動態載入器將匯入符號(例如,函式Func的地址)的地址填入到匯入符號地址表中。然而,當其它目標檔案中的彙編程式碼執行call Func時,它們期待的是用Func來命名那段code的地址。但我們直到執行時的時候才知道這個地址:我們能夠靜態地得知的事情只有動態連結器會將這個地址存放在哪個地方。我們稱這個地方為_imp_Func。
為了處理這一層額外的中間層,匯入庫匯出的函式Func僅僅間接引用了_imp_Func(來獲得實際的函式指標),然後執行jmp跳轉到它。同一個工程中的所有其它的目標檔案現在可以呼叫call Func,彷彿Func已經在其它目標檔案而不是其它DLL中定義過了一樣。基於這個原因,動態連結的函式的宣告上的__declspec(dllimport)只是可有可無的(儘管實際上如果加上這個修飾符的話,程式碼的效率會有輕微的提升,我們之後會談到這一點)。
不幸的是,如果你想要從另一個DLL中匯入變數,則並沒有類似的技巧。如果我們有一個匯入的變數myData,並沒有一種方法來定義一個匯入庫,使得連結到這個匯入庫的目標檔案可以通過執行mov $eax, myData來寫入到myData所在的記憶體位置。取而代之的是,匯入庫定義了一個符號__imp__myData,這個符號解析到一個可以找到myData連結地址的地方。然後編譯器就會保證當你在讀寫用__declspec(dllimport)定義的變數時,這些讀寫其實是通過__imp_myData來間接進行的。因為需要在使用的時候再產生不同的程式碼,因此在匯入變數時的__declspec宣告是不可省去的。
應用例項
理論都很好,但在實踐中看看所有這些的這些部分會對我們很有幫助。
構建DLL
首先,讓我們來構建一個簡單的DLL,同時匯出了函式和變數。為了最大化地進行說明,我們將使用顯式地匯出庫,而不是用declspec(dllexport)來修飾我們的函式,也不是提供一個.def檔案給連結器。
先建立一個.def檔案,library.def:
1 2 3 4
| LIBRARY library EXPORTS function_export data_export DATA
|
(DATA關鍵詞和LIBRARY這一行僅僅影響到匯入庫如何被產生,之後本文會解釋這一點。現在請暫時忽略這個。)
然後構建一個匯出檔案:
1
| $ dlltool --output-exp library_exports.o -d library.def
|
產生的目標檔案基本上只包含了一個.edata段,匯出了符號_data_export和_function_export,名稱分別是data_export和function_export:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| $ objdump -xs library_exports.o
...
There is an export table in .edata at 0x0
The Export Tables (interpreted .edata section contents)
Export Flags 0 Time/Date stamp 4e10e5c1 Major/Minor 0/0 Name 00000028 library_exports.o.dll Ordinal Base 1 Number in: Export Address Table 00000002 [Name Pointer/Ordinal] Table 00000002 Table Addresses Export Address Table 00000040 Name Pointer Table 00000048 Ordinal Table 00000050
Export Address Table -- Ordinal Base 1
[Ordinal/Name Pointer] Table [ 0] data_export [ 1] function_export
Sections: Idx Name Size VMA LMA File off Algn 0 .text 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, READONLY, CODE 1 .data 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, DATA 2 .bss 00000000 00000000 00000000 00000000 2**2 ALLOC 3 .edata 00000070 00000000 00000000 000000b4 2**2 CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA SYMBOL TABLE: [ 0](sec -2)(fl 0x00)(ty 0)(scl 103) (nx 1) 0x00000000 fake File [ 2](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000028 name [ 3](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000040 afuncs [ 4](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000048 anames [ 5](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000050 anords [ 6](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000054 n1 [ 7](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000060 n2 [ 8](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .text AUX scnlen 0x0 nreloc 0 nlnno 0 [ 10](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .data AUX scnlen 0x0 nreloc 0 nlnno 0 [ 12](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .bss AUX scnlen 0x0 nreloc 0 nlnno 0 [ 14](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .edata AUX scnlen 0x70 nreloc 8 nlnno 0 [ 16](sec 0)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 _data_export [ 17](sec 0)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 _function_export
RELOCATION RECORDS FOR [.edata]: OFFSET TYPE VALUE 0000000c rva32 .edata 0000001c rva32 .edata 00000020 rva32 .edata 00000024 rva32 .edata 00000040 rva32 _data_export 00000044 rva32 _function_export 00000048 rva32 .edata 0000004c rva32 .edata
Contents of section .edata: 0000 00000000 c1e5104e 00000000 28000000 .......N....(... 0010 01000000 02000000 02000000 40000000 ............@... 0020 48000000 50000000 6c696272 6172795f H...P...library_ 0030 6578706f 7274732e 6f2e646c 6c000000 exports.o.dll... 0040 00000000 00000000 54000000 60000000 ........T...`... 0050 00000100 64617461 5f657870 6f727400 ....data_export. 0060 66756e63 74696f6e 5f657870 6f727400 function_export.
|
我們將會用一個簡單的DLL實現來提供這些符號,library.c:
1 2 3 4 5
| int data_export = 42;
int function_export() { return 1337 + data_export; }
|
打包到一個DLL中:
1
| $ gcc -shared -o library.dll library.c library_exports.o
|
這個DLL的匯出符號表如下,可見我們已經匯出了我們所需的符號資訊:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| The Export Tables (interpreted .edata section contents)
Export Flags 0 Time/Date stamp 4e10e5c1 Major/Minor 0/0 Name 00005028 library_exports.o.dll Ordinal Base 1 Number in: Export Address Table 00000002 [Name Pointer/Ordinal] Table 00000002 Table Addresses Export Address Table 00005040 Name Pointer Table 00005048 Ordinal Table 00005050
Export Address Table -- Ordinal Base 1 [ 0] +base[ 1] 200c Export RVA [ 1] +base[ 2] 10f0 Export RVA
[Ordinal/Name Pointer] Table [ 0] data_export [ 1] function_export
|
使用DLL
當我們回過頭來看看如何使用DLL時,事情變得更有有趣了。首先,我們需要一個匯出庫:
1
| $ dlltool --output-lib library.dll.a -d library.def
|
(我們使用匯入庫library.dll.a而不是直接使用匯出符號的物件檔案library_exports.o,是因為使用庫來匯入允許連結器忽略.idata段中並沒有被使用到的符號。而相反的是連結器無法忽略.edata段中的任何符號,因為任何一個符號都可能被這個DLL的使用者用到)。
匯入庫是相當複雜的。對於每一個匯入符號,匯入庫中都包含一個對應的目標檔案(disds00000.o和disds00001.o),同時也包含了其它兩個目標檔案(distdt.o和disdh.o),用於設立匯入列表的頭部和尾部。(匯入列表的頭部除了其它的一些東西,還包含了在執行時需要連結的DLL的名字,這是從.def檔案的LIBRARY一行派生而來的。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
| $ objdump -xs library.dll.a In archive library.dll.a:
disdt.o: file format pe-i386
...
Sections: Idx Name Size VMA LMA File off Algn 0 .text 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, READONLY, CODE 1 .data 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, DATA 2 .bss 00000000 00000000 00000000 00000000 2**2 ALLOC 3 .idata$4 00000004 00000000 00000000 00000104 2**2 CONTENTS, ALLOC, LOAD, DATA 4 .idata$5 00000004 00000000 00000000 00000108 2**2 CONTENTS, ALLOC, LOAD, DATA 5 .idata$7 0000000c 00000000 00000000 0000010c 2**2 CONTENTS, ALLOC, LOAD, DATA SYMBOL TABLE: [ 0](sec -2)(fl 0x00)(ty 0)(scl 103) (nx 1) 0x00000000 fake File [ 2](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .text AUX scnlen 0x0 nreloc 0 nlnno 0 [ 4](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .data AUX scnlen 0x0 nreloc 0 nlnno 0 [ 6](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .bss AUX scnlen 0x0 nreloc 0 nlnno 0 [ 8](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .idata$4 AUX scnlen 0x4 nreloc 0 nlnno 0 [ 10](sec 5)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .idata$5 AUX scnlen 0x4 nreloc 0 nlnno 0 [ 12](sec 6)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .idata$7 AUX scnlen 0x7 nreloc 0 nlnno 0 [ 14](sec 6)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 __library_dll_a_iname
Contents of section .idata$4: 0000 00000000 .... Contents of section .idata$5: 0000 00000000 .... Contents of section .idata$7: 0000 6c696272 6172792e 646c6c00 library.dll.
disdh.o: file format pe-i386
...
Sections: Idx Name Size VMA LMA File off Algn 0 .text 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, READONLY, CODE 1 .data 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, DATA 2 .bss 00000000 00000000 00000000 00000000 2**2 ALLOC 3 .idata$2 00000014 00000000 00000000 00000104 2**2 CONTENTS, ALLOC, LOAD, RELOC, DATA 4 .idata$5 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, DATA 5 .idata$4 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, DATA SYMBOL TABLE: [ 0](sec -2)(fl 0x00)(ty 0)(scl 103) (nx 1) 0x00000000 fake File [ 2](sec 6)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 hname [ 3](sec 5)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 fthunk [ 4](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .text AUX scnlen 0x0 nreloc 0 nlnno 0 [ 6](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .data AUX scnlen 0x0 nreloc 0 nlnno 0 [ 8](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .bss AUX scnlen 0x0 nreloc 0 nlnno 0 [ 10](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x00000000 .idata$2 AUX scnlen 0x14 nreloc 3 nlnno 0 [ 12](sec 6)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .idata$4 [ 13](sec 5)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .idata$5 [ 14](sec 4)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 __head_library_dll_a [ 15](sec 0)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 __library_dll_a_iname
RELOCATION RECORDS FOR [.idata$2]: OFFSET TYPE VALUE 00000000 rva32 .idata$4 0000000c rva32 __library_dll_a_iname 00000010 rva32 .idata$5
Contents of section .idata$2: 0000 00000000 00000000 00000000 00000000 ................ 0010 00000000 ....
disds00001.o: file format pe-i386
...
Sections: Idx Name Size VMA LMA File off Algn 0 .text 00000008 00000000 00000000 0000012c 2**2 CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 1 .data 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, DATA 2 .bss 00000000 00000000 00000000 00000000 2**2 ALLOC 3 .idata$7 00000004 00000000 00000000 00000134 2**2 CONTENTS, RELOC 4 .idata$5 00000004 00000000 00000000 00000138 2**2 CONTENTS, RELOC 5 .idata$4 00000004 00000000 00000000 0000013c 2**2 CONTENTS, RELOC 6 .idata$6 00000012 00000000 00000000 00000140 2**1 CONTENTS SYMBOL TABLE: [ 0](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .text [ 1](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .data [ 2](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .bss [ 3](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .idata$7 [ 4](sec 5)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .idata$5 [ 5](sec 6)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .idata$4 [ 6](sec 7)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .idata$6 [ 7](sec 1)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 _function_export [ 8](sec 5)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 __imp__function_export [ 9](sec 0)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 __head_library_dll_a
RELOCATION RECORDS FOR [.text]: OFFSET TYPE VALUE 00000002 dir32 .idata$5
RELOCATION RECORDS FOR [.idata$7]: OFFSET TYPE VALUE 00000000 rva32 __head_library_dll_a
RELOCATION RECORDS FOR [.idata$5]: OFFSET TYPE VALUE 00000000 rva32 .idata$6
RELOCATION RECORDS FOR [.idata$4]: OFFSET TYPE VALUE 00000000 rva32 .idata$6
Contents of section .text: 0000 ff250000 00009090 .%...... Contents of section .idata$7: 0000 00000000 .... Contents of section .idata$5: 0000 00000000 .... Contents of section .idata$4: 0000 00000000 .... Contents of section .idata$6: 0000 01006675 6e637469 6f6e5f65 78706f72 ..function_expor 0010 7400 t.
disds00000.o: file format pe-i386
...
Sections: Idx Name Size VMA LMA File off Algn 0 .text 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, READONLY, CODE 1 .data 00000000 00000000 00000000 00000000 2**2 ALLOC, LOAD, DATA 2 .bss 00000000 00000000 00000000 00000000 2**2 ALLOC 3 .idata$7 00000004 00000000 00000000 0000012c 2**2 CONTENTS, RELOC 4 .idata$5 00000004 00000000 00000000 00000130 2**2 CONTENTS, RELOC 5 .idata$4 00000004 00000000 00000000 00000134 2**2 CONTENTS, RELOC 6 .idata$6 0000000e 00000000 00000000 00000138 2**1 CONTENTS SYMBOL TABLE: [ 0](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .text [ 1](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .data [ 2](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .bss [ 3](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .idata$7 [ 4](sec 5)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .idata$5 [ 5](sec 6)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .idata$4 [ 6](sec 7)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 .idata$6 [ 7](sec 5)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 __imp__data_export [ 8](sec 0)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 __head_library_dll_a
RELOCATION RECORDS FOR [.idata$7]: OFFSET TYPE VALUE 00000000 rva32 __head_library_dll_a
RELOCATION RECORDS FOR [.idata$5]: OFFSET TYPE VALUE 00000000 rva32 .idata$6
RELOCATION RECORDS FOR [.idata$4]: OFFSET TYPE VALUE 00000000 rva32 .idata$6
Contents of section .idata$7: 0000 00000000 .... Contents of section .idata$5: 0000 00000000 .... Contents of section .idata$4: 0000 00000000 .... Contents of section .idata$6: 0000 00006461 74615f65 78706f72 7400 ..data_export.
|
注意data_export對應的目標包含一個空的.text段,然而function_export卻有定義一些程式碼。如果我們反彙編就會看到:
1 2 3 4 5
| 00000000 <_function_export>: 0: ff 25 00 00 00 00 jmp *0x0 2: dir32 .idata$5 6: 90 nop 7: 90 nop
|
型別dir32的重定位告訴連結器如何填寫被jmp間接引用的地址。我們可以看到當進入_function_export時,會直接跳到從名為.idata$5的記憶體處讀取的地址。通過徹底地檢查.idata段,可以發現.idata$5對應的是匯入地址表中function_export這個匯入名稱所對應的地址,於是就能找到載入的匯入項function_export的絕對地址。
雖然只有function_export擁有一個對應的_function_export函式,但是以上的兩個匯入項在匯入庫中分別對應了一個符號,這個符號的名稱帶有__imp__字首(__imp__data_export和__imp__function_export)。就像我們之前探討過的那樣,這個符號代表了一個記憶體地址,這個地址中存放的是的指向函式或變數的指標,這個指標值由動態連結器負責填寫。
通過一個匯入庫,我們就可以寫一個使用這些匯出函式的程式碼,例如這個main1.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <stdio.h>
__declspec(dllimport) extern int function_export(void); __declspec(dllimport) extern int data_export;
int main(int argc, char **argv) { printf("%d\n", function_export()); printf("%d\n", data_export);
data_export++;
printf("%d\n", function_export()); printf("%d\n", data_export);
return 0; }
|
編譯這段程式碼並連線匯入庫,我們就會得到我們期待的結果:
1 2 3 4 5
| $ gcc main1.c library.dll.a -o main1 && ./main1 1379 42 1380 43
|
之所以library.dll.a內沒有定義data_export符號而這段程式碼仍能編譯,是因為main.c檔案中的data_export宣告上的__declspec(dllimport)修飾符導致編譯器生成了直接使用__imp_data_export符號的程式碼,反彙編的話我們就會看到:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| $ gcc -c main1.c -o main1.o && objdump --disassemble -r main1.o
main1.o: file format pe-i386
Disassembly of section .text:
00000000 <_main>: 0: 8d 4c 24 04 lea 0x4(%esp),%ecx 4: 83 e4 f0 and $0xfffffff0,%esp 7: ff 71 fc pushl -0x4(%ecx) a: 55 push %ebp b: 89 e5 mov %esp,%ebp d: 51 push %ecx e: 83 ec 14 sub $0x14,%esp 11: e8 00 00 00 00 call 16 <_main+0x16> 12: DISP32 ___main 16: a1 00 00 00 00 mov 0x0,%eax 17: dir32 __imp__function_export 1b: ff d0 call *%eax 1d: 89 44 24 04 mov %eax,0x4(%esp) 21: c7 04 24 00 00 00 00 movl $0x0,(%esp) 24: dir32 .rdata 28: e8 00 00 00 00 call 2d <_main+0x2d> 29: DISP32 _printf 2d: a1 00 00 00 00 mov 0x0,%eax 2e: dir32 __imp__data_export 32: 8b 00 mov (%eax),%eax 34: 89 44 24 04 mov %eax,0x4(%esp) 38: c7 04 24 00 00 00 00 movl $0x0,(%esp) 3b: dir32 .rdata 3f: e8 00 00 00 00 call 44 <_main+0x44> 40: DISP32 _printf 44: a1 00 00 00 00 mov 0x0,%eax 45: dir32 __imp__data_export 49: 8b 00 mov (%eax),%eax 4b: 8d 50 01 lea 0x1(%eax),%edx 4e: a1 00 00 00 00 mov 0x0,%eax 4f: dir32 __imp__data_export 53: 89 10 mov %edx,(%eax) 55: a1 00 00 00 00 mov 0x0,%eax 56: dir32 __imp__function_export 5a: ff d0 call *%eax 5c: 89 44 24 04 mov %eax,0x4(%esp) 60: c7 04 24 00 00 00 00 movl $0x0,(%esp) 63: dir32 .rdata 67: e8 00 00 00 00 call 6c <_main+0x6c> 68: DISP32 _printf 6c: a1 00 00 00 00 mov 0x0,%eax 6d: dir32 __imp__data_export 71: 8b 00 mov (%eax),%eax 73: 89 44 24 04 mov %eax,0x4(%esp) 77: c7 04 24 00 00 00 00 movl $0x0,(%esp) 7a: dir32 .rdata 7e: e8 00 00 00 00 call 83 <_main+0x83> 7f: DISP32 _printf 83: b8 00 00 00 00 mov $0x0,%eax 88: 83 c4 14 add $0x14,%esp 8b: 59 pop %ecx 8c: 5d pop %ebp 8d: 8d 61 fc lea -0x4(%ecx),%esp 90: c3 ret 91: 90 nop 92: 90 nop 93: 90 nop
|
實際上,我們可以看到生成的程式碼甚至都沒有使用_function_export符號,取而代之的是使用了imp__function_export。本質上,匯入庫中的_function_export符號在每處使用的地方都已經被內聯過了。這也就是為什麼使用__declspec(dllimport)可以提高跨DLL呼叫的效能,不過這個修飾符在宣告函式時不是必須寫的。
我們也許會好奇,如果在宣告時去掉__declspec(dllimport)修飾符會發生什麼事情。鑑於我們之前討論的關於匯入變數和匯入函式之間的差別,你也許以為會連結失敗。我們的測試檔案main2.c是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <stdio.h>
extern int function_export(void); extern int data_export;
int main(int argc, char **argv) { printf("%d\n", function_export()); printf("%d\n", data_export);
data_export++;
printf("%d\n", function_export()); printf("%d\n", data_export);
return 0; }
|
讓我們來試一試:
1 2 3 4 5
| $ gcc main2.c library.dll.a -o main2 && ./main2 1379 42 1380 43
|
見鬼了!編譯居然通過了?這有點令人驚訝。之所以匯入庫library.dll.a沒有定義_data_export符號但這仍能編譯通過,是由於GNU ld的一個叫做自動匯入的有趣的特性。如果沒有自動匯入特性,連結器就會如我們所願地報錯:
1 2 3 4 5 6
| $ gcc main2.c library.dll.a -o main2 -Wl,--disable-auto-import && ./main2 /tmp/ccGd8Urx.o:main2.c:(.text+0x2c): undefined reference to `_data_export' /tmp/ccGd8Urx.o:main2.c:(.text+0x41): undefined reference to `_data_export' /tmp/ccGd8Urx.o:main2.c:(.text+0x49): undefined reference to `_data_export' /tmp/ccGd8Urx.o:main2.c:(.text+0x63): undefined reference to `_data_export' collect2: ld returned 1 exit status
|
微軟的連結器沒有實現自動匯入的特性,因此如果你用的是微軟的工具鏈的話,你就會看到類似的錯誤資訊。
然而,有一個方法可以使得在寫程式碼時既不用依賴於自動匯入的特定,也不用使用__declspec(dllimport)關鍵字。我們新的程式碼main3.c就是這麼寫的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <stdio.h>
extern int (*_imp__function_export)(void); extern int *_imp__data_export;
#define function_export (*_imp__function_export) #define data_export (*_imp__data_export)
int main(int argc, char **argv) { printf("%d\n", function_export()); printf("%d\n", data_export);
data_export++;
printf("%d\n", function_export()); printf("%d\n", data_export);
return 0; }
|
在這段程式碼中,我們直接使用了源自匯入庫中的帶__imp__字首的符號。這些符號對應的是匯入函式和匯入變數的真實記憶體地址,就像程式碼中的預處理巨集定義data_export和function_export所表示的那樣。
即使沒有自動編譯特性,這段程式碼也能完美地編譯通過:
1 2 3 4 5
| $ gcc main3.c library.dll.a -o main3 -Wl,--disable-auto-import && ./main3 1379 42 1380 43
|
如果你一直閱讀到了這裡,你應該已經對Windows上上DLL的匯入和匯出有了透徹的理解。
本文地址:https://xnerv.wang/zh-tw/everything-you-never-wanted-to-know-about-dlls-cn/