Suppose we have process A and processB, both are using example.dll.
Now let's suppose that this dll was mapped to different addresses in process A and process B(say, it's due to ASLR or some other conflict).
Will the operating system map it twice or will it still be able to use the same physical page where the dll was mapped for both processes? I mean, that's the whole point of a DLL right? shared memory so we don't have to map things twice.
You already got all the right answers from the comments, I just want to demonstrate it from a kernel debugger (Windbg).
I have notepad.exe and explorer.exe running.
EPROCESSstructure (this is the structure that describes a process from the kernel point of view, this is the 64-bit number afterPROCESS):Notice the CR3 of the process which is given by DirBase, here:
0x16543002.We have the module bases, ends and their names. On Windows, ASLR is per boot (not per process). So, in all processes
ntdllwill be mapped at the same address until the system is rebooted and new random address is chosen.ntdllmodule:Typical PE module header (
MZandThis program cannot ...).!vtop(Virtual to Physical) requires the base of the PML4 table - for the given process - and the virtual address to be translated (thus, we need to set the lower 12 bits of CR3 to 0 since there are not used for the address of the PML4 base):Now let's try with explorer.exe:
As you can see we get the same resulting physical address. Note that the table entries (PML4E, PDPTE, PDE and PTE) are not the same in both processes, but the resulting physical page is exactly the same.
Obviously, Windows has a CoW (Copy-on-Write) mechanisms that takes place if you'd write to one of those pages: it would be immediately copied and only the process where the write had happened would see the modified page.