Remix.run Logo
201984 2 hours ago

Shared libraries (and mmapped files in general) are deduplicated; it's nowhere near as bad as you think. The kernel loads a .so into memory once and then maps that memory into every process that mmaps it.

Editing to add: this deduplication is one of the greatest upsides to dynamic linking. Common libs like libgcc and libc only have to exist in memory once and can stay in CPU caches, whereas if they were statically linked into every binary, each binary would have a copy of that library that wouldn't be shared with anything else and you'd waste a lot of memory.

sjmulder an hour ago | parent [-]

Doesn't the loaded code have to be patched for relocations?

ptspts an hour ago | parent | next [-]

It does, so not 100% is reused. The patched parts are in different sections though, so the entire .text (code) section ends up being reused.

monocasa an hour ago | parent | prev | next [-]

Not on modern archs that provide decent support for PIE (position independent executables).

201984 20 minutes ago | parent [-]

How do you think position independent code can call functions from other .so's without being patched with their addresses?

They can't, so even PIC code still has to have a relocation table that gets patched. It's in a different page than the code though, so code does still get reused.

t-3 an hour ago | parent | prev [-]

Not if it's position-independent.