Question

If there is a shared object file say libComponent.so which is made up of two object files Component_1.o and Compononet_2.o.

And there is an application which links to libComponent.so but is only using Compononent_1.o functions.

Will the entire shared object i.e libComponent.so will be loaded into memory when application runs and uses shared object file or just the Component_1.o ?

Is there an option available in gcc compiler to toggle this behaviour of only loading the required symbols from a shared object ?

Was it helpful?

Solution

Well, it depends on what you mean by 'loaded'.

The dynamic linker will map all of the library into the process's virtual memory space and will fill in entries in the executable's import table for each library function used with the addresses of functions in the shared library. But filling in the import table doesn't actually load from those addresses, so they won't be loaded into physical memory.

From then on, the library code will be paged into physical memory on demand when the function is called, just like any other pageable memory in the process's virtual address space. If a function is never called (directly from the application or indirectly from another library function called by the application), it won't be paged in. (Well, paging occurs with page size granularity, so you might pull in a function the application doesn't call if it's next to a function it does call. Some compilers use profile-guided optimization to place functions commonly called together next to each other to minimize the number of pages used.)

(Aside: if your library wasn't compiled to use position-independent code and it's loaded at its non-default base address, the linker will need to fix up addresses in the code when it's loaded, which would cause the entire library to be paged in. This could be done lazily when each page is first loaded, though I'm not sure which linkers do this.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top