简体   繁体   English

C、C++:共享库:是单个函数还是完整的库加载到 memory 中?

[英]C, C++: Shared libraries: Are single functions or complete libraries loaded into memory?

With static compilation, only the functions of a library which are actually needed by a program are linked to the program.通过static编译,只有程序实际需要的库函数才链接到程序。 How is that with shared libraries?共享库如何? Are only the functions actually needed by a program are loaded into memory by the dynamic linker, or is always the total shared library loaded?是否只有程序实际需要的功能由动态 linker 加载到 memory 中,还是总是加载整个共享库? If it is functions, how could I get the actual size of a program including its loaded functions during runtime?如果是函数,我怎么能得到一个程序的实际大小,包括它在运行时加载的函数?

Thank You !谢谢你 ! Oliver奥利弗

With static compilation, only the functions of a library which are actually needed by a program are linked to the program.通过static编译,只有程序实际需要的库函数才链接到程序。 How is that with shared libraries?共享库如何?

Shared libraries are referenced by the program symbolically, that is, the program will identify, by name, the shared library it was linked with.共享库由程序象征性地引用,也就是说,程序将通过名称识别它所链接的共享库。

Are only the functions actually needed by a program are loaded into memory by the dynamic linker, or is always the total shared library loaded?是否只有程序实际需要的功能由动态 linker 加载到 memory 中,还是总是加载整个共享库?

The program will reference specific entry points and data objects in the shared library.该程序将引用共享库中的特定入口点和数据对象。 The shared library will be mapped into memory as a single large object, but only the pages that are actually referenced will be paged in by the kernel.共享库将作为单个大 object映射到 memory,但只有实际引用的页面将由 kernel 分页。 The total amount of the library that gets loaded will depend on both the density of references, references by other images linked to it, and by the locality of the library's own functionality.加载的库的总量将取决于引用的密度、链接到它的其他图像的引用以及库自身功能的位置。

If it is functions, how could I get the actual size of a program including its loaded functions during runtime?如果是函数,我怎么能得到一个程序的实际大小,包括它在运行时加载的函数?

The best way on Mac and other Unix-based systems is with ps(1) .在 Mac 和其他基于 Unix 的系统上,最好的方法是使用ps(1)

When you link statically, only the functions that are (potentially) called get linked into the executable -- but at run-time, the data from the executable file will be read into memory by demand paging.当您静态链接时,只有(可能)调用的函数才会链接到可执行文件中——但在运行时,来自可执行文件的数据将通过请求分页读取到 memory 中。

When the process is created, addresses are assigned to all the code in the executable and shared libraries for that process, but the code/data from the file isn't necessarily read into physical memory at that time.创建进程时,将为该进程的可执行库和共享库中的所有代码分配地址,但此时文件中的代码/数据不一定会读入物理 memory。 When you attempt to access an address that's not currently in physical memory, it'll trigger a not-present exception.当您尝试访问当前不在物理 memory 中的地址时,它将触发不存在的异常。 The OS virtual memory manager will react to that by reading the page from the file into physical memory, than letting the access proceed.操作系统虚拟 memory 管理器将对此做出反应,将文件中的页面读入物理 memory,而不是让访问继续进行。

The loading is done on a page-by-page basis, which usually means blocks of 4 or 8 kilobytes at a time (eg, x86 uses 4K pages, Alpha used 8k).加载是逐页完成的,这通常意味着一次 4 或 8 KB 的块(例如,x86 使用 4K 页面,Alpha 使用 8k)。 x86 does also have an ability to create larger (4 megabyte) pages, but those aren't (at least usually) used for normal code -- they're for mapping big blocks of memory that remain mapped (semi-)permanently, such as the "window" of memory on a typical graphics card that's also mapped so it's directly accessible by he CPU. x86 也具有创建更大(4 兆字节)页面的能力,但那些(至少通常)不用于普通代码——它们用于映射 memory 的大块,这些块保持映射(半)永久,例如作为典型图形卡上 memory 的“窗口”,该图形卡也已映射,因此 CPU 可以直接访问它。

Most loaders have some optimizations, so (for example) they'll attempt to read bigger blocks of memory when the program starts up initially.大多数加载器都有一些优化,因此(例如)它们会在程序最初启动时尝试读取更大的 memory 块。 This lets it start faster than if there was an interrupt and separate read for each page of code as it's accessed.这使它比在访问每页代码时发生中断和单独读取时更快地启动。 The exact details of that optimization vary between OSes (and often even versions of the same OS).该优化的确切细节因操作系统(通常甚至同一操作系统的版本)而异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM