The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: AeroASM on May 12, 2005, 05:42:30 PM

Title: DLLs and Virtual Memory
Post by: AeroASM on May 12, 2005, 05:42:30 PM
"In Windows there is only one copy of a DLL in memory, but Windows through the magic of paging makes it appear that each application has its own copy."

How? I have spent ages trying to figure it out but I can't
Title: Re: DLLs and Virtual Memory
Post by: pbrennick on May 12, 2005, 08:02:07 PM
Aero,
A dll when loaded by an application is loaded into the flat memory used by the application.  Each applicat:ion loads its own dlls.  There is no paging.

Paul
Title: Re: DLLs and Virtual Memory
Post by: AeroASM on May 12, 2005, 08:34:57 PM
But isn't the point of a DLL that there is only one copy, shared by all the EXE's?
If not, then what is the point? Why not just use static libraries?
Title: Re: DLLs and Virtual Memory
Post by: Jibz on May 12, 2005, 08:55:07 PM
The read-only parts of a dll are shared amongst processes. Other parts are copy-on-write so each process gets it's own copy if it modifies the pages.

It's all handled through the memory manager, which translates virtual addresses into physical ones.
Title: Re: DLLs and Virtual Memory
Post by: Opcode on May 12, 2005, 11:14:15 PM
Quote from: AeroASM on May 12, 2005, 08:34:57 PM
But isn't the point of a DLL that there is only one copy, shared by all the EXE's?
If not, then what is the point? Why not just use static libraries?

Hi AeroASM,

I see that you are very interested in how windows manages the memory
internally.
I really suggest you to read this online book, specially this chapter:
http://www.windowsitlibrary.com/Content/356/04/toc.html

It has the precise answer to your question.

Regards,
Opcode
Title: Re: DLLs and Virtual Memory
Post by: pbrennick on May 13, 2005, 12:24:17 AM
Aero,
DLLs are shared by processes and if you have loaded an exe as a child, it will access the same DLL because a child uses the same resources as the parent.  We can get the handle of another exe that is not a child and communicate with it but I have never heard of this going any deeper.

Quote
But isn't the point of a DLL that there is only one copy, shared by all the EXE's?
If not, then what is the point? Why not just use static libraries?

Don't confuse system DLLs and user DLLs...

Paul
Title: Re: DLLs and Virtual Memory
Post by: Gustav on May 13, 2005, 06:16:37 AM
> it will access the same DLL because a child uses the same resources as the parent

I don't aggree. Win32 allows file handles to be inherited, but that is it. Other resources are not shared in Win32 between "child" and "parent". Dll read-only pages may be shared among processes, yes, but that has nothing to do with any child-parent relationship.
Title: Re: DLLs and Virtual Memory
Post by: hutch-- on May 13, 2005, 06:46:57 AM
The loading of a DLL within an applications workspace is not as simple as it sounds because another DLL may have been already loaded at the first DLLs preferred load address. The OS relocates the DLL to another load address so when you have multiple applcations using the same DLL, it may not alway be loaded at the same address in each applcations address space.

In this context the code is not shared but duplicated at another load address.
Title: Re: DLLs and Virtual Memory
Post by: AeroASM on May 13, 2005, 06:56:59 AM
I have just found out about the .reloc section and things are making a little bit more sense now; I am going to read Opcode's article.
Title: Re: DLLs and Virtual Memory
Post by: pbrennick on May 13, 2005, 11:27:24 AM
Gustav,
That portion of my reply, parent-child relationships, was in reference to Applications loaded by the Parent.  In that context, the child inherits from the parent and that is just the way it is.

Sorry I confused you.
Paul
Title: Re: DLLs and Virtual Memory
Post by: Gustav on May 14, 2005, 06:45:39 AM

Paul,

you didn't confuse me. IMO your statement was wrong and I told you so. But we surely can both live with our opinions.
Title: Re: DLLs and Virtual Memory
Post by: Jibz on May 14, 2005, 07:34:16 AM
Quote from: pbrennickDLLs are shared by processes and if you have loaded an exe as a child, it will access the same DLL because a child uses the same resources as the parent.

Not sure what you mean by 'same'. The child may get the same DLL files loaded, but it will have it's own 'copy' with sharing as mentioned :U.

Quote from: hutch--In this context the code is not shared but duplicated at another load address.

It should only affect the pages that need relocating.