TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Jonathan de Boyne Pollard
from: Herbert Rosenau
date: 1998-09-24 10:44:24
subject: How do DLLs load and unload ?

Hi Jonathan,

 JdBP> How are DLLs loaded and unloaded ?  In particular, how does OS/2 Warp
 JdBP> keep track of which DLL_InitTerm functions it needs to call when
 JdBP> executing DosFreeModule ?

It's no question!

Each DLL has only *one* DLL_InitTerm(). This DLL is called twice:
- with the parameter flag == 0 - for Init once per process
- with the parameter flag == 1 - for Term once per process

#pragma linkage (_DLL_InitTerm, system)
unsigned long _DLL_InitTerm(unsigned long modhandle, unsigned long flag) {

	if (flag) {
           /* any code for termination goes here */
           _CRT_term(0UL);  /* let the C runtime cleaninmg up */
	   return(1);       /* termination is finished */
	} else {
	   /* any code for init goes here */
	   if (_CRT_init() == -1) /* init the C runtime */
              return(0UL);  /* something goes wrong, break down! */
           /* now we can use any C function we will */
	   return(1);  /* init is well done */
}

 JdBP>   It seems to me that there is a chicken-and-egg problem here.  It cannot
 JdBP> know which InitTerm functions to call before it starts unloading modules
 JdBP> from the address space, but it cannot unload the modules from the address
 JdBP> space since they must remain mapped so that their InitTerm functions can
 JdBP> be called.  How does OS/2 solve this ?

Each DLL can only have ONE InitTerm - the linker can't handle more than one
function with the same name.

 JdBP> If you are about to tell me that OS/2 uses callbacks into ring 3 from
 JdBP> ring 0 in order to call the InitTerm functions, how does it *return* from
 JdBP> those callbacks into ring 0 ?

What the problem?

You asks the system to load a program:

- the loader loads it and dereferences all external addresses. Thereby it
my find a DLL to load. this is done by an DosLoadModule() - recursive if
possible.
- if the DLL is already in memory only the shared data is new allocated and
then _Dll_InitTerm() is called.

If you use DosLoadModule() to load a dynamic DLL the linker does the same.

The loader links/unlinks external addresses for the process the dll is
loaded/freed before/after InitTerm is called.

Tschau/Bye

Herbert Rosenau



--- Sqed/32 1.14/development
* Origin: SOLINET ist fuer alle Arbeitnehmer da (2:2476/493.1)
SEEN-BY: 396/1 632/0 371 633/210 260 267 270 371 635/506 728 639/252 670/218
@PATH: 2476/493 480 2410/200 2432/200 2433/1200 225 270/101 396/1 633/260
@PATH: 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.