Linux 和 Windows 使用不同的策略来访问存储在动态库中的数据。
在 Linux 上,对对象的未定义引用在链接时解析为库。链接器查找对象的大小并在可执行文件的.bss 或.rdata 段中为其保留空间。执行时,动态链接器 (ld.so) 将符号解析为动态库(再次),并将对象从动态库复制到进程的内存。
在 Windows 上,对对象的未定义引用在链接时解析为导入库,并且没有为其保留空间。执行模块时,动态链接器将符号解析为动态库,并在进程中创建一个写入内存映射副本,由动态库中的共享数据段支持。
copy on write memory map 的优点是如果链接的数据没有改变,那么它可以与其他进程共享。在实践中,这是一个微不足道的好处,它大大增加了工具链和使用动态库的程序的复杂性。对于实际编写的对象,这总是效率较低。
我怀疑,虽然我没有证据,但这个决定是针对一个特定的并且现在已经过时的用例做出的。在 16 位 Windows 上(在官方 Microsoft 程序或其他程序中)使用动态库中的大型(当时)只读对象可能是一种常见的做法。无论哪种方式,我怀疑 Microsoft 的任何人现在都有专业知识和时间来改变它。
为了调查这个问题,我创建了一个从动态库写入对象的程序。它在对象中每页写入一个字节(4096 字节),然后写入整个对象,然后重试每页写入的初始一个字节。如果在调用main 之前为进程保留对象,则第一个和第三个循环的时间应该大致相同,而第二个循环的时间应该比两者都长。如果对象是写入映射到动态库的副本,则第一个循环所用的时间至少应与第二个一样长,而第三个循环所用的时间应少于两者。
结果与我的假设一致,反汇编分析证实 Linux 在链接时间地址访问动态库数据,相对于程序计数器。令人惊讶的是,Windows 不仅间接访问数据,每次循环迭代都会从导入地址表中重新加载指向数据的指针及其长度,并启用优化。这是在 Windows XP 上使用 Visual Studio 2010 进行测试的,所以也许事情已经改变了,虽然我不认为它已经改变了。
以下是 Linux 的结果:
$ dd bs=1M count=16 if=/dev/urandom of=libdat.dat
$ xxd -i libdat.dat libdat.c
$ gcc -O3 -g -shared -fPIC libdat.c -o libdat.so
$ gcc -O3 -g -no-pie -L. -ldat dat.c -o dat
$ LD_LIBRARY_PATH=. ./dat
local = 0x1601060
libdat_dat = 0x601040
libdat_dat_len = 0x601020
dirty= 461us write= 12184us retry= 456us
$ nm dat
[...]
0000000000601040 B libdat_dat
0000000000601020 B libdat_dat_len
0000000001601060 B local
[...]
$ objdump -d -j.text dat
[...]
400693: 8b 35 87 09 20 00 mov 0x200987(%rip),%esi # 601020 <libdat_dat_len>
[...]
4006a3: 31 c0 xor %eax,%eax # zero loop counter
4006a5: 48 8d 15 94 09 20 00 lea 0x200994(%rip),%rdx # 601040 <libdat_dat>
4006ac: 0f 1f 40 00 nopl 0x0(%rax) # align loop for efficiency
4006b0: 89 c1 mov %eax,%ecx # store data offset in ecx
4006b2: 05 00 10 00 00 add $0x1000,%eax # add PAGESIZE to data offset
4006b7: c6 04 0a 00 movb $0x0,(%rdx,%rcx,1) # write a zero byte to data
4006bb: 39 f0 cmp %esi,%eax # test loop condition
4006bd: 72 f1 jb 4006b0 <main+0x30> # continue loop if data is left
[...]
以下是 Windows 的结果:
$ cl /Ox /Zi /LD libdat.c /link /EXPORT:libdat_dat /EXPORT:libdat_dat_len
[...]
$ cl /Ox /Zi dat.c libdat.lib
[...]
$ dat.exe # note low resolution timer means retry is too small to measure
local = 0041EEA0
libdat_dat = 1000E000
libdat_dat_len = 1100E000
dirty= 20312us write= 3125us retry= 0us
$ dumpbin /symbols dat.exe
[...]
9000 .data
1000 .idata
5000 .rdata
1000 .reloc
17000 .text
[...]
$ dumpbin /disasm dat.exe
[...]
004010BA: 33 C0 xor eax,eax # zero loop counter
[...]
004010C0: 8B 15 8C 63 42 00 mov edx,dword ptr [__imp__libdat_dat] # store data pointer in edx
004010C6: C6 04 02 00 mov byte ptr [edx+eax],0 # write a zero byte to data
004010CA: 8B 0D 88 63 42 00 mov ecx,dword ptr [__imp__libdat_dat_len] # store data length in ecx
004010D0: 05 00 10 00 00 add eax,1000h # add PAGESIZE to data offset
004010D5: 3B 01 cmp eax,dword ptr [ecx] # test loop condition
004010D7: 72 E7 jb 004010C0 # continue loop if data is left
[...]
这是用于两个测试的源代码:
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
typedef FILETIME time_l;
time_l time_get(void) {
FILETIME ret; GetSystemTimeAsFileTime(&ret); return ret;
}
long long int time_diff(time_l const *c1, time_l const *c2) {
return 1LL*c2->dwLowDateTime/100-c1->dwLowDateTime/100+c2->dwHighDateTime*100000-c1->dwHighDateTime*100000;
}
#else
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
typedef struct timespec time_l;
time_l time_get(void) {
time_l ret; clock_gettime(CLOCK_MONOTONIC, &ret); return ret;
}
long long int time_diff(time_l const *c1, time_l const *c2) {
return 1LL*c2->tv_nsec/1000-c1->tv_nsec/1000+c2->tv_sec*1000000-c1->tv_sec*1000000;
}
#endif
#ifndef PAGESIZE
#define PAGESIZE 4096
#endif
#ifdef _WIN32
#define DLLIMPORT __declspec(dllimport)
#else
#define DLLIMPORT
#endif
extern DLLIMPORT unsigned char volatile libdat_dat[];
extern DLLIMPORT unsigned int libdat_dat_len;
unsigned int local[4096];
int main(void) {
unsigned int i;
time_l t1, t2, t3, t4;
long long int d1, d2, d3;
t1 = time_get();
for(i=0; i < libdat_dat_len; i+=PAGESIZE) {
libdat_dat[i] = 0;
}
t2 = time_get();
for(i=0; i < libdat_dat_len; i++) {
libdat_dat[i] = 0xFF;
}
t3 = time_get();
for(i=0; i < libdat_dat_len; i+=PAGESIZE) {
libdat_dat[i] = 0;
}
t4 = time_get();
d1 = time_diff(&t1, &t2);
d2 = time_diff(&t2, &t3);
d3 = time_diff(&t3, &t4);
printf("%-15s= %18p\n%-15s= %18p\n%-15s= %18p\n", "local", local, "libdat_dat", libdat_dat, "libdat_dat_len", &libdat_dat_len);
printf("dirty=%9lldus write=%9lldus retry=%9lldus\n", d1, d2, d3);
return 0;
}
我真诚地希望其他人能从我的研究中受益。感谢阅读!