Dynamic Link Libraries, DLLs Explained

blueelvis

BSOD Kernel Dump Senior Analyst
Joined
Apr 14, 2014
Posts
970
Location
India


Dynamic Link Library
? Some of you might say that seriously what am I talking about but you would be amazed to know that the Dynamic Link Libraries play a very important role in the proper functioning of the Operating System and other Win32 Applications. I decided to write this article because of the fact that I recently had a missing DLL Error when trying to run Deus Ex Human Revolution which was related to Steam and was fixed simply by rechecking the files using Steam.

DLL-File-Error.jpg


So let us begin the journey by understanding the meaning of the DLL or the Dynamic Link Library. In terms of Computers, Dynamic refers to the fact that it can be loaded/unloaded into/from the memory while the application is running. Link means that there is a linkage between more than one file (Generally, the executable and DLL Files). And Library means that it is a place where the functions are defined and can be called in the application which has loaded them. The DLL is Microsoft's implementation of the shared library concept in the Microsoft Windows.
Before we proceed further, we must note that the Dynamic Link Libraries (DLLs) generally have an extension of "dll" but can be of other extensions as well like the "ocx" (Libraries for containing ActiveX Controls), "drv" (for legacy system drivers) or even "sys" (Export Drivers which I would be talking about a bit as well below).


So basically, the question arises that what is a DLL File?
The answer to this question is that it is nothing but function definitions along with declaration which perform operations depending on what is defined inside those functions.


I think it would generate more interest if we check out the advantages and disadvantages of the DLL Files.

Advantages :

1. Saves memory and reduces swapping.

2. Saves Disk Space.

3. Upgrades to the applications is easy.

4. Debugging and Bug Fixing is eased if the code is available in the DLL since the application is not needed to be re-compiled. This is true only if the function's parameters and return types are not changed.

Disadvantages:

• The only disadvantage I could think up was that the application becomes dependent on the existence of the DLL file else it won't run.


• Malicious DLL's can be loaded as well which is very popular these days.

Let's discuss the advantages now. Let's start with the point number 1 in the advantages. For understanding this point, we need to understand the basic concepts of how applications run. Whenever an application runs, the source code is copied to the memory. The variables which store the data are given a different set of address space (Stack if declared statically and Heap if declared dynamically) whereas the functions which process the data & deal with variables are allocated with memory from Stack. When a function is called inside the application, the call is made to the functions inside the DLL File. These functions are also stored in the memory. Windows makes sure that at any given time, only one copy of the Dynamic Link Library is loaded into the memory for access purposes and the system makes sure that the other copies of the DLLs are not loaded. The system maintains a per-process reference count for each DLL. When a thread loads the DLL, the reference count is incremented by one. When the process terminates, or when the reference count becomes zero, the DLL is unloaded from the virtual address space of the process. Some DLLs and executables which are required by the Windows Operating System, are kept in the memory constantly and cannot be unloaded so even if there reference count reaches zero, they are not unloaded. So, using DLLs saves the Memory.

Let's talk about the point number 2 now which is how using DLLs we can save the storage space. Suppose that I develop 4 programs using a DLL which leverage the power of the functions from "d3dx9_43.dll" in order to utilize Direct X. Now, I am not required to integrate the code of the given DLL file into each of my application. Just bundle the setup and the DLL file. Have the Setup of all the 4 applications to the same location on the File System and reference the DLLs from there. Hence, saving the storage space.

I think that the points 3 & 4 should be explained simultaneously. Suppose you create 3 modules for a single application. You observe that the Application is having problem in a particular area, so you can easily find the problem in the DLL file which is handling that part of the application. Also, suppose I have created 2 applications in Visual C++ and the other 2 applications are created in Visual C# but the DLL is written in VB.Net . Don't worry, we can use the same DLL File in such cases no matter the language in which it is written in it and which application is calling it. (As long as the programs follow the function's calling convention)

A DLL can define two kinds of functions: exported and internal. The exported functions are intended to be called by other modules, as well as from within the DLL where they are defined. Internal functions are typically intended to be called only from within the DLL where they are defined. Although a DLL can export data, its data is generally used only by its functions. However, there is nothing to prevent another module from reading or writing that address.

The DLL files can be loaded into either the User Space of the Memory or the Kernel Space of the memory. The DLL Files loaded into the Kernel Space cannot access any of the functions which are present in the User Mode/Space or the User Mode API's. The vice-versa of this is also true. Now, the question might arise that another module can write at the address of Kernel Mode from User Mode but the Operating System keeps a check on who is accessing whose memory address space.



Now, into The Further we go...


Let's pick up the question if this same can be achieved for the Drivers or not. In the most fundamental sense, a driver is a software component that lets the operating system and a device communicate with each other. For example, suppose an application needs to read some data from a device. The application calls a function implemented by the operating system, and the operating system calls a function implemented by the driver.


The behaviour of the Dynamic Link Libraries can be achieved in Drivers as well if the Driver is compiled as Export. The Kernel Mode Driver DLL has an extension of "sys" and basically behaves the same as the previously discussed Dynamically Linked Libraries, the only difference being that it works for the Drivers and is present in the Kernel Mode. If you ask that why can't I just load a DLL file from User Mode (Extension : DLL) into the driver / kernel space is the same as stated above that the Operating System (Windows) does not allow this. This is required to ensure the security, integrity and confidentiality of the information. The driver compiled using this would behave the same as the normal DLL Files and the functions which are declared that can be imported are available for other drivers to use.


I hope this helps you. The next time you encounter the missing DLL, you would know why it is required. In my next post, I would be describing the process of creating a Dynamic Linked Library and how to use it.


Thanks for reading ^_^.

References :



Feedback would be much appreciated like if there is any typo or you would like me to add something more or simply anything ^_^. Also, I hope this is the correct section for posting this :)
 
Some additional information for you to add:

File Signature Information for .DLL:

Code:
4D 5A ASCII - MZ

File Signature Database: DLL File Signatures

IAT and EAT would be good data structures to start looking at too.

Harry,

What would be the use of having a DLL Signature like the one you posted?

I will take a look at those data structures as well once I finish writing the post on the creation of DLL Files ^_^
 
It allows to identify the type of file, some malware will attempt to poorly disguise itself as a .PDF file etc.
 
The one thing about DLLs that stands out for me was reading about "DLL HELL" - although I was not around at the time (using Windows extensively) to "enjoy" it. :)

http://en.wikipedia.org/wiki/DLL_Hell

I've heard from others who lived through it that it was in fact pure hell at times.
 
It allows to identify the type of file, some malware will attempt to poorly disguise itself as a .PDF file etc.

Hmm, that is interesting. Thanks for the information as usual Harry ^_^

The one thing about DLLs that stands out for me was reading about "DLL HELL" - although I was not around at the time (using Windows extensively) to "enjoy" it. :)

DLL Hell - Wikipedia, the free encyclopedia

I've heard from others who lived through it that it was in fact pure hell at times.
That is interesting but I don't see it happen these days or the storage capacity is very high and the techniques are improving.

Also John, would it pose any problem if I post this exact same post on my blog as well?
 
Windows makes sure that at any given time, only one copy of the Dynamic Link Library is loaded into the memory for access purposes and the system makes sure that the other copies of the DLLs are not loaded

kindly can you explain this point? same dll referenced by multiple executables or same exectable having multiple dlls ? under which condition windows make sure one one copy of DLL is loaded into moemory and which dll referenced here?
 
kindly can you explain this point? same dll referenced by multiple executables or same exectable having multiple dlls ? under which condition windows make sure one one copy of DLL is loaded into moemory and which dll referenced here?

Both. Most applications load multiple dlls, but what is particular here is that any one dll is only loaded once system wide (as long as it's exactly the same version etc.)

Perhaps application a.exe loads a big CRT library: lib.dll and a networking library net.dll (and probably many, many more).
Then b.exe loads exactly the same CRT library lib.dll and a crypto library crypt.dll.

It'd be very wasteful to load lib.dll and all of its dependencies, and dependencies many times into memory - almost as many times as there are applications on your system.

So instead lib.dll is loaded only once, and used by both a.exe and b.exe.
 
Did this help to reduce "DLL Hell" that I've read about?

Also, aren't most DLL libraries statically linked, which kind of defeats the purpose of DLLs to begin with as the EXE now contains the DLL library, whereas before, the DLL library was dynamically linked (which led to DLL Hell) by the EXE?

I may have the wrong term here - "dynamically linked ". What I mean is that a DLL is loaded and the EXE refers to it during execution. In fact multiple EXEs can refer to a single DLL which was assumed by all EXEs to be a certain size, date, version, etc... when in fact it often was not the version the EXE needed because the DLL had been overwritten by an update (could be a lower or higher version number).
 
Last edited:
Did this help to reduce "DLL Hell" that I've read about?

Also, aren't most DLL libraries statically linked, which kind of defeats the purpose of DLLs to begin with as the EXE now contains the DLL library, whereas before, the DLL library was dynamically linked (which led to DLL Hell) by the EXE?

I may have the wrong term here - "dynamically linked ". What I mean is that a DLL is loaded and the EXE refers to it during execution. In fact multiple EXEs can refer to a single DLL which was assumed by all EXEs to be a certain size, date, version, etc... when in fact it often was not the version the EXE needed because the DLL had been overwritten by an update (could be a lower or higher version number).

DLL hell was fixed by WinSxS, where Windows keeps track of and stores lots of different versions of the same DLL, and serves the correct version DLL to each different application, rather than a battle between installers to overwrite the copy in System32 with their version of the DLL (DLL hell).

Your static linking vs dynamic linking terminology is perfect. But it is not common. Static linking is very rare and in most cases strongly discouraged.
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top