Sysinternals Homepage
Forum Home Forum Home > Windows Discussions > Internals
  New Posts New Posts RSS Feed - Sharing memory of DLL Code (or not?)
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Sharing memory of DLL Code (or not?)

 Post Reply Post Reply
Author
Message
JDlugosz View Drop Down
Groupie
Groupie


Joined: 14 December 2005
Location: United States
Status: Offline
Points: 92
Post Options Post Options   Thanks (0) Thanks(0)   Quote JDlugosz Quote  Post ReplyReply Direct Link To This Post Topic: Sharing memory of DLL Code (or not?)
    Posted: 29 November 2010 at 6:55pm
I recall that Windows will share physical pages for a DLL that is loaded by multiple processes, with a copy-on-write mode.  The use of jump tables for DLL imports is, in part, to localize changes and prevent patching the whole thing.

But, what about local function calls within a DLL?  If the DLL was not "based" at a unique address as a separate build step (or during installation), it will never be loaded at its default address.  This would, I would think, cause every internal CALL instruction to be patched, and most code is filled with calls to other functions.

So, does Windows optimize that somehow, and notice that the needed DLL is already loaded at some other address, and see if this new process can also use the DLL at the same address?

Or, does DLL code end up not sharing physical pages at all?

My immediate practical concern is wondering whether properly "basing" everything as part of installation of a complex application suite would save physical memory demands.

--John
Back to Top
naylon View Drop Down
Newbie
Newbie


Joined: 31 December 2010
Status: Offline
Points: 2
Post Options Post Options   Thanks (0) Thanks(0)   Quote naylon Quote  Post ReplyReply Direct Link To This Post Posted: 01 January 2011 at 2:01pm
function address = base address + offset
 
The base address is different, but the offset wouldn't change.
 
Myabe I didn't understood what you said, I'm a Chinese, my English is poor, forgive me:)
Back to Top
hux View Drop Down
Groupie
Groupie


Joined: 08 January 2011
Location: Germany
Status: Offline
Points: 50
Post Options Post Options   Thanks (0) Thanks(0)   Quote hux Quote  Post ReplyReply Direct Link To This Post Posted: 09 January 2011 at 12:14am
Hi JDlugosz,

almost all addresses in call or jmp instructions are relative. These do not need any modification by the loader regardless of the final base address in memory.  If a binary uses any absolut addresses, it has to list the corresponding instructions in the PE's relocation table. If the loader cannot load the binary at the preferred base address, it patches the intructions specified in the relocation table. Since copy-on-write is used for the text segment, all pages that get modified in this process are not shared between applications. All other pages ARE shared. Furthermore, the image of the process's executable can always be loaded at the preferred base address and the memory consequently can always be shared, too. This of course does not take ASLR into consideration.

Nonetheless, you are right that choosing proper base addresses for own libraries is a good practice as it (depending on the binary) can save memory and reduce load time (no relocation necessary). For that reason, all DLLs that ship with Windows use optimized (different) image bases. 
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down