2
votes

I am having problem in understanding following statement that i read from Microsoft .It says that "Multiple processes that load the same DLL at the same base address share a single copy of the DLL in physical memory."

SO what i understand is if a DLL has to be shared between different processes then that DLL has to loaded in the same base address in every processes virtual memory .Why this ? What happens if the process is loaded at different base addresses in the different processes virtual address space ,since ultimately all the process will be accessing the same physical memory in the end for the shared DLL .

2

2 Answers

3
votes

image section bind to file on disk. while we not modify some page from section set - this page will be shared and backed by image file. but if we modify page - it can not be more backed by image file. system need allocate new physical page for this modified page. this page already will be private for process and backed by page file.

are section mapped at the same or different address not play direct role. but in case image section - if it mapped not at common preferred address - it must be relocated. relocation - require code modification. as result image pages modification, allocation new private physical pages

3
votes

The reason is dynamic linking. Most DLL files are not just loaded into memory, they are processed so that absolute or long jumps inside the code are rewritten to make sense. THEN the pages are locked for read-only+execute purposes.

So if the same DLL is loaded at different base addresses in different processes, the results of the dynamic linking process will be different, and the memory pages cannot be shared.

This is true for the DLL file data, e.g. code. The memory which is allocated / used while the DLL code runs is of course something completely different.