ObjectFileLoader.st
changeset 107 d9a681b9508e
parent 106 2653f74569d1
child 108 dc930059fbc9
--- a/ObjectFileLoader.st	Sun Aug 13 22:52:47 1995 +0200
+++ b/ObjectFileLoader.st	Tue Aug 15 13:45:23 1995 +0200
@@ -12,7 +12,8 @@
 
 Object subclass:#ObjectFileLoader
        instanceVariableNames:''
-       classVariableNames:'MySymbolTable Verbose LastError LoadedObjects NextHandleID'
+       classVariableNames:'MySymbolTable Verbose LastError LoadedObjects
+			   NextHandleID PreviouslyLoadedObjects'
        poolDictionaries:''
        category:'System-Compiler'
 !
@@ -21,7 +22,7 @@
 COPYRIGHT (c) 1993 by Claus Gittinger
 	     All Rights Reserved
 
-$Header: /cvs/stx/stx/libcomp/ObjectFileLoader.st,v 1.32 1995-08-13 20:52:28 claus Exp $
+$Header: /cvs/stx/stx/libcomp/ObjectFileLoader.st,v 1.33 1995-08-15 11:45:23 claus Exp $
 '!
 
 !ObjectFileLoader class methodsFor:'documentation'!
@@ -42,7 +43,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libcomp/ObjectFileLoader.st,v 1.32 1995-08-13 20:52:28 claus Exp $
+$Header: /cvs/stx/stx/libcomp/ObjectFileLoader.st,v 1.33 1995-08-15 11:45:23 claus Exp $
 "
 !
 
@@ -194,6 +195,11 @@
     MySymbolTable := 'smalltalk'.
     Verbose := false.
     NextHandleID := 1.
+    ObjectMemory addDependent:self.
+
+    "
+     ObjectFileLoader initialize
+    "
 !
 
 verbose:aBoolean
@@ -204,6 +210,19 @@
     "ObjectFileLoader verbose:true"
 ! !
 
+!ObjectFileLoader class methodsFor:'change & update'!
+
+update:something
+    "sent, when image is saved or restarted"
+
+    (something == #aboutToSnapshot) ifTrue:[
+	self unloadAndRememberAllObjectFiles
+    ].
+    (something == #restarted) ifTrue:[
+	self reloadAllRememberedObjectFiles
+    ].
+! !
+
 !ObjectFileLoader class methodsFor:'command defaults'!
 
 needSeparateIDSpaces
@@ -637,7 +656,7 @@
 
 
     Verbose ifTrue:[
-	Transcript showCr:('looking for undefs ...').
+	'looking for undefs ...' infoPrintNL.
     ].
 
     "
@@ -662,7 +681,7 @@
 
     initAddr notNil ifTrue:[
 	Verbose ifTrue:[
-	    Transcript showCr:'calling init at: ' , (initAddr printStringRadix:16)
+	    ('calling init at: ' , (initAddr printStringRadix:16)) infoPrintNL.
 	].
 	status := self performModuleInitAt:initAddr identifyAs:handle.
 	"
@@ -702,7 +721,7 @@
     ].
 
     Verbose ifTrue:[
-	Transcript showCr:('no symbol: ', symName,' in ',aFileName).
+	('no symbol: ', symName,' in ',aFileName) infoPrintNL.
     ].
 
     "
@@ -711,16 +730,16 @@
     moreHandles notNil ifTrue:[
 	moreHandles do:[:aHandle |
 	    Verbose ifTrue:[
-		Transcript showCr:('unloading: ', aHandle printString).
+		('unloading: ', aHandle printString) infoPrintNL.
 	    ].
-	    self closeDynamicObject:handle.
+	    self unloadDynamicObject:handle.
 	]
     ].
 
     Verbose ifTrue:[
-	Transcript showCr:('unloading: ', handle printString).
+	('unloading: ', handle printString) infoPrintNL.
     ].
-    self closeDynamicObject:handle.
+    self unloadDynamicObject:handle.
     ^ nil
 
     "
@@ -787,7 +806,7 @@
     ].
     initAddr notNil ifTrue:[
 	Verbose ifTrue:[
-	    Transcript showCr:'calling init at:' , (initAddr printStringRadix:16)
+	    ('calling init at:' , (initAddr printStringRadix:16)) infoPrintNL.
 	].
 	status := self performModuleInitAt:initAddr identifyAs:handle.
 	didInit := true.
@@ -796,7 +815,7 @@
 	 look for init-function(s); call them all
 	"
 	Verbose ifTrue:[
-	    Transcript showCr:'no init found; looking for candidates ...'
+	    'no init found; looking for candidates ...' infoPrintNL.
 	].
 	initNames := self namesMatching:'*_Init' segment:'[tT]' in:aFileName.
 	initNames notNil ifTrue:[
@@ -812,7 +831,7 @@
 		    ^ false
 		].
 		Verbose ifTrue:[
-		    Transcript showCr:'calling init at:' , (initAddr printStringRadix:16)
+		    ('calling init at:' , (initAddr printStringRadix:16)) infoPrintNL
 		].
 		self performModuleInitAt:initAddr identifyAs:handle.
 		didInit := true.
@@ -855,7 +874,7 @@
 list size == 1 ifTrue:[
 "/    (self isCPlusPlusObject:handle) ifTrue:[
 	Verbose ifTrue:[
-	    Transcript showCr:'a c++ object file'
+	    'a c++ object file' infoPrintNL.
 	].
 	"
 	 what I would like to get is the CTOR_LIST,
@@ -868,7 +887,7 @@
 
 "/        initAddr := self getFunction:'__CTOR_LIST__' from:handle.
 "/        Verbose ifTrue:[
-"/            Transcript showCr:'calling CTORs at:' , (initAddr printStringRadix:16)
+"/            ('calling CTORs at:' , (initAddr printStringRadix:16)) infoPrintNL
 "/        ].
 
 	initAddr := self getFunction:list first from:handle.
@@ -886,13 +905,13 @@
 	].
 	initAddr isNil ifTrue:[
 	    Verbose ifTrue:[
-		Transcript showCr:'no CTOR-func found (' , list first , ')'
+		('no CTOR-func found (' , list first , ')') infoPrintNL.
 	    ].
-	    self closeDynamicObject:aFileName.
+	    self unloadDynamicObject:aFileName.
 	    ^ nil
 	].
 	Verbose ifTrue:[
-	    Transcript showCr:'calling CTORs at:' , (initAddr printStringRadix:16)
+	    ('calling CTORs at:' , (initAddr printStringRadix:16)) infoPrintNL
 	].
 	self callInitFunctionAt:initAddr 
 	     specialInit:false 
@@ -902,7 +921,7 @@
 	     identifyAs:nil.
 
 	Verbose ifTrue:[
-	    Transcript showCr:'done with CTORs.'
+	    'done with CTORs.' infoPrintNL
 	].
 
 	"
@@ -914,9 +933,9 @@
 
 
     Verbose ifTrue:[
-	Transcript showCr:'unknown object file'
+	'unknown object file' infoPrintNL
     ].
-    self closeDynamicObject:aFileName.
+    self unloadDynamicObject:aFileName.
     ^ nil
 !
 
@@ -979,7 +998,7 @@
 
     "/ unload ...
 
-    self closeDynamicObject:handle
+    self unloadDynamicObject:handle
 !
 
 unloadAllObjectFiles
@@ -987,7 +1006,7 @@
      see DANGER ALERT in ObjectFileLoader>>unloadObjectFile:"
 
     LoadedObjects notNil ifTrue:[
-	LoadedObjects allKeysDo:[:aFileName |
+	LoadedObjects keys copy do:[:aFileName |
 	    self unloadObjectFile:aFileName
 	]
     ].
@@ -995,6 +1014,31 @@
     "
      ObjectFileLoader unloadAllObjectFiles
     "
+!
+
+unloadAndRememberAllObjectFiles
+    PreviouslyLoadedObjects := LoadedObjects keys copy.
+    self unloadAllObjectFiles
+!
+
+reloadAllRememberedObjectFiles
+    PreviouslyLoadedObjects do:[:fileName |
+	self loadObjectFile:fileName
+    ].
+    PreviouslyLoadedObjects := nil
+! !
+
+!ObjectFileLoader class methodsFor:'queries'!
+
+loadedObjectFiles
+    "return a collection containing the names of all
+     dynamically loaded objects."
+
+    ^ LoadedObjects keys copy
+
+    "
+     ObjectFileLoader loadedObjectFiles
+    "
 ! !
 
 !ObjectFileLoader class methodsFor:'dynamic object access'!
@@ -1009,7 +1053,7 @@
     |handle buffer|
 
     Verbose ifTrue:[
-	Transcript showCr:'loadDynamic: ' , pathName , ' ...'
+	('loadDynamic: ' , pathName , ' ...') infoPrintNL
     ].
 
     "/ already loaded ?
@@ -1018,7 +1062,7 @@
 	handle := LoadedObjects at:pathName ifAbsent:nil.
 	handle notNil ifTrue:[
 	    Verbose ifTrue:[
-		Transcript showCr:'... ' , pathName , ' already loaded.'.
+		('... ' , pathName , ' already loaded.') infoPrintNL.
 	    ].
 	    ^ handle
 	].
@@ -1037,7 +1081,7 @@
     handle isNil ifTrue:[
 	LastError == #notImplemented ifTrue:[
 	    Verbose ifTrue:[
-		Transcript showCr:'no dynamic load facility or load failed.'.
+		'no dynamic load facility or load failed.' infoPrintNL.
 		Transcript showCr:'Try to load it myself ...'.
 	    ].
 	    "try it the hard way"
@@ -1058,7 +1102,7 @@
     Smalltalk flushCachedClasses.
 
     Verbose ifTrue:[
-	Transcript showCr:'loadDynamic ok; handle is: ' , handle printString.
+	('loadDynamic ok; handle is: ' , handle printString) infoPrintNL.
     ].
 
     ^ handle
@@ -1278,7 +1322,7 @@
     ^ nil
 !
 
-closeDynamicObject:handle
+unloadDynamicObject:handle
     "close an object-file (unmap from my address space)
      and remove the entry from the remembered object file set.
      This is a low-level entry, which does not care if there are
@@ -1288,7 +1332,9 @@
 
     |key|
 
-    (self primCloseDynamicObject:handle) ifTrue:[
+    self performModuleDeInit:handle.
+
+    (self primUnloadDynamicObject:handle) ifTrue:[
 	"
 	 remove from loaded objects
 	"
@@ -1300,12 +1346,12 @@
 	].
 	Smalltalk flushCachedClasses
     ] ifFalse:[
-	self error:'closeDynamic failed'
+	self error:'unloadDynamic failed'
     ]
 !
 
-primCloseDynamicObject:aHandle
-    "close an object-file (unmap from my address space).
+primUnloadDynamicObject:aHandle
+    "unload an object-file (unmap from my address space).
      This is a low-level entry, which does not care if there are
      still any code references (from blocks or methods) to this
      module. Calling it for still living classes will definitely
@@ -1443,7 +1489,7 @@
     [p atEnd] whileFalse:[
 	entry := p nextLine.
 	Verbose ifTrue:[
-	    entry printNL.
+	    entry infoPrintNL.
 	].
 	entry notNil ifTrue:[
 	    s := ReadStream on:entry.
@@ -1455,12 +1501,12 @@
 		    (segmentPattern isNil or:[segmentPattern match:segment]) ifTrue:[
 			l add:name.
 			Verbose ifTrue:[
-			    'found name: ' print. name printNL.
+			    ('found name: ' , name) infoPrintNL.
 			]
 		    ] ifFalse:[
 			Verbose ifTrue:[
-			    name print. ' segment mismatch ' print.
-			    segmentPattern print. ' ' print. segment printNL.
+			    name infoPrint. ' segment mismatch ' infoPrint.
+			    segmentPattern infoPrint. ' ' infoPrint. segment infoPrintNL.
 			]
 		    ]
 		]
@@ -1589,13 +1635,15 @@
 		printf("get sym <%s> handle = %x\n", __stringVal(aString), h);
 	    addr = dlsym(h, (char *) __stringVal(aString));
 	    if (addr) {
-		if (ObjectFileLoader_Verbose == true)
-		    printf("addr = %x\n", addr);
+		if (ObjectFileLoader_Verbose == true) {
+		    printf("dlsym %s ok; addr = %x\n", addr);
+		}
 		lowAddr = _MKSMALLINT( (int)addr & 0xFFFF );
 		hiAddr = _MKSMALLINT( ((int)addr >> 16) & 0xFFFF );
 	    } else {
-		if (ObjectFileLoader_Verbose == true)
-		    printf("dlsym %s error: %s\n", __stringVal(aString), dlerror());
+		if (ObjectFileLoader_Verbose == true) {
+		    printf("dlsym %s error: %s", __stringVal(aString), dlerror());
+		}
 	    }
 	}
     }
@@ -1778,6 +1826,15 @@
 %}
 !
 
+performModuleDeInit:handle
+    |id|
+
+    id := handle at:4.
+%{
+    __UNREGISTER_BY_ID(__intVal(id));
+%}
+!
+
 performModuleInitAt:initAddr identifyAs:handle
     "
      Initialize a smalltalk module.
@@ -1795,7 +1852,7 @@
     "/ and define its globals
     "/
     Verbose ifTrue:[
-	Transcript showCr:'phase 0 ...'
+	'phase 0 ...' infoPrintNL
     ].
     self callInitFunctionAt:initAddr 
 	 specialInit:true 
@@ -1827,17 +1884,17 @@
     "/ remaining initialization
     "/
     Verbose ifTrue:[
-	Transcript showCr:'phase 1 ...'
+	'phase 1 ...' infoPrintNL
     ].
     self moduleInit:1 forceOld:true interruptable:false.
 
     Verbose ifTrue:[
-	Transcript showCr:'phase 2 ...'
+	'phase 2 ...' infoPrintNL
     ].
     self moduleInit:2 forceOld:true interruptable:false.
 
     Verbose ifTrue:[
-	Transcript showCr:'phase 3 ...'
+	'phase 3 ...' infoPrintNL
     ].
     self moduleInit:3 forceOld:false interruptable:true.
     ^ Array with:#ok
@@ -1901,7 +1958,7 @@
     hi := address // 16r10000.
     low := address \\ 16r10000.
     handle notNil ifTrue:[
-        moduleID := handle at:4
+	moduleID := handle at:4
     ].
 %{
     void (*addr)();