vanangamudi
vanangamudi

Reputation: 727

loading and dynamic linking

When a process requires a function from a dynamic library, is the whole library loaded into memory or just the function alone is loaded?

For example: When I use the printf() or any function from the libc (assuming it is a dynamic library), is the whole libc loaded into memory or just the printf() function alone?

Upvotes: 2

Views: 80

Answers (1)

Employed Russian
Employed Russian

Reputation: 213375

is the whole library loaded into memory or just the function alone is loaded?

The answer depends on what exactly you mean by "loaded into memory", and on which operating system you are running your program.

Let's consider Linux, and typical linking against libc.so.

There, the entire PT_LOAD segments containing .data and .text of libc.so.6 are mmap()ed into memory before the first instruction of your program even executes (because your program records that it needs libc.so.6 in its .dynamic section).

From then on, the code is demand paged into RAM, when you access it. When you call printf, the pages that contain code for printf are paged in from disk, or (more likely) reused from the buffer cache.

Upvotes: 1

Related Questions