我开始在 Linux 中编写更多代码,并尝试更好地感受与 Windows 非常不同的环境/API。无论如何,我正在涉足共享库 .so(与 Windows .dll 相比),并注意到当共享对象加载到内存中时,它比磁盘上的大 7752 字节,令人惊讶?我期望磁盘上的图像与内存中的图像匹配,或者下面的演示代码中可能存在错误?
来自 godbolt 的示例显示了以下输出:
Program returned: 0
Program stdout
Loaded: linux-vdso.so.1
Loaded: /lib/x86_64-linux-gnu/libdl.so.2
Loaded: /lib/x86_64-linux-gnu/libc.so.6
---------------------MEMORY-------------------------
libc.so.6 size: 2037344 bytes
7f67c7429000 7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00 .ELF............
7f67c7429010 03 00 3e 00 01 00 00 00 c0 41 02 00 00 00 00 00 ..>......A......
7f67c7429020 40 00 00 00 00 00 00 00 18 e7 1e 00 00 00 00 00 @...............
...
7f67c761a630 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
7f67c761a640 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
7f67c761a650 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
---------------------DISK-------------------------
libc.so.6 size: 2029592 bytes
7f67c7236010 7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00 .ELF............
7f67c7236020 03 00 3e 00 01 00 00 00 c0 41 02 00 00 00 00 00 ..>......A......
7f67c7236030 40 00 00 00 00 00 00 00 18 e7 1e 00 00 00 00 00 @...............
...
7f67c74257f8 00 00 00 00 00 00 00 00 c8 e2 1e 00 00 00 00 00 ................
7f67c7425808 4b 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 K...............
7f67c7425818 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
问题
我很好奇为什么磁盘与内存之间的共享库大小存在差异?
它可以使用相当大的.bss部分(静态变量)