0
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
45
|
3 |
All Rights Reserved
|
0
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Object subclass:#ObjectFileLoader
|
|
14 |
instanceVariableNames:''
|
107
|
15 |
classVariableNames:'MySymbolTable Verbose LastError LoadedObjects
|
|
16 |
NextHandleID PreviouslyLoadedObjects'
|
0
|
17 |
poolDictionaries:''
|
|
18 |
category:'System-Compiler'
|
|
19 |
!
|
|
20 |
|
|
21 |
ObjectFileLoader comment:'
|
|
22 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
45
|
23 |
All Rights Reserved
|
28
|
24 |
|
113
|
25 |
$Header: /cvs/stx/stx/libcomp/Attic/ObjFLoader.st,v 1.38 1995-08-27 23:24:55 claus Exp $
|
0
|
26 |
'!
|
|
27 |
|
47
|
28 |
!ObjectFileLoader class methodsFor:'documentation'!
|
|
29 |
|
|
30 |
copyright
|
|
31 |
"
|
|
32 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
33 |
All Rights Reserved
|
|
34 |
|
|
35 |
This software is furnished under a license and may be used
|
|
36 |
only in accordance with the terms of that license and with the
|
|
37 |
inclusion of the above copyright notice. This software may not
|
|
38 |
be provided or otherwise made available to, or used by, any
|
|
39 |
other person. No title to or ownership of the software is
|
|
40 |
hereby transferred.
|
|
41 |
"
|
|
42 |
!
|
|
43 |
|
|
44 |
version
|
|
45 |
"
|
113
|
46 |
$Header: /cvs/stx/stx/libcomp/Attic/ObjFLoader.st,v 1.38 1995-08-27 23:24:55 claus Exp $
|
47
|
47 |
"
|
|
48 |
!
|
|
49 |
|
|
50 |
documentation
|
|
51 |
"
|
|
52 |
This class knowns how to dynamically load in external object-modules.
|
90
|
53 |
|
|
54 |
WARNING:
|
|
55 |
As to date, this is completely experimental and WITHOUT ANY WARRANTY.
|
|
56 |
It is still being developed and the code below needs cleanup and more
|
|
57 |
robustness.
|
47
|
58 |
|
|
59 |
There are basically two totally different mechanisms to do this:
|
|
60 |
a) if there exists some dynamic-link facility such as:
|
|
61 |
GNU-DL, dlopen (sparc, SYS5.4), rld_open (NeXT),
|
|
62 |
this is used
|
|
63 |
b) if no such facility exists, the normal linker is used to
|
|
64 |
link the module to text/data address as previously malloced,
|
|
65 |
and the object file loaded into that space.
|
|
66 |
|
|
67 |
Currently, not all mechanisms work fully satisfying.
|
|
68 |
For example, the sun dl*-functions do an exit on link-errors (which
|
|
69 |
is certainly not what we want here :-(; the NeXT mechanism does not
|
|
70 |
allow for selective unloading (only all or last).
|
82
|
71 |
The only really useful packages are the GNU-dl package and the SGI/Unixware
|
|
72 |
sys5.4 libdl packages.
|
|
73 |
The GNU-dl package is only available for a.out file formats;
|
|
74 |
i.e. only (a subset of) linux people can use it at this time.
|
95
|
75 |
|
|
76 |
Once stable, the functionality contained herein will be moved into
|
|
77 |
the VM.
|
|
78 |
(It is needed there, to allow reloading of objectfiles upon
|
|
79 |
image restart; i.e. before any class is reinitialilized).
|
47
|
80 |
"
|
|
81 |
! !
|
|
82 |
|
53
|
83 |
!ObjectFileLoader primitiveDefinitions!
|
47
|
84 |
|
0
|
85 |
%{
|
15
|
86 |
/*
|
|
87 |
* by default, use whatever the system provides
|
|
88 |
*/
|
95
|
89 |
#ifdef sunos /* sunos (pre 5.4) dlopen interface */
|
15
|
90 |
# define SUN_DL
|
20
|
91 |
# define HAS_DL
|
15
|
92 |
#endif
|
|
93 |
|
95
|
94 |
#ifdef NeXT /* next rld interface */
|
15
|
95 |
# define NEXT_DL
|
20
|
96 |
# define HAS_DL
|
15
|
97 |
#endif
|
|
98 |
|
95
|
99 |
#ifdef SYSV4 /* sys5.4 dlopen interface */
|
15
|
100 |
# define SYSV4_DL
|
20
|
101 |
# define HAS_DL
|
15
|
102 |
#endif
|
|
103 |
|
95
|
104 |
#ifdef DL1_6 /* dl1.6 COFF loader */
|
|
105 |
# define HAS_DL
|
|
106 |
#endif
|
|
107 |
|
98
|
108 |
#ifdef _AIX
|
97
|
109 |
# define AIX_DL
|
|
110 |
# define HAS_DL
|
|
111 |
#endif
|
|
112 |
|
15
|
113 |
/*
|
20
|
114 |
* but GNU_DL overwrites this - its better
|
15
|
115 |
*/
|
95
|
116 |
#ifdef GNU_DL /* dld2.3.x interface */
|
20
|
117 |
# define HAS_DL
|
15
|
118 |
# undef SYSV4_DL
|
|
119 |
# undef NEXT_DL
|
|
120 |
# undef SUN_DL
|
95
|
121 |
# undef DL1_6
|
15
|
122 |
#endif
|
|
123 |
|
20
|
124 |
#include <stdio.h>
|
|
125 |
|
|
126 |
/*
|
95
|
127 |
* if no dynamic link facilities, try it the hard way ...
|
20
|
128 |
*/
|
|
129 |
#ifndef HAS_DL
|
|
130 |
|
|
131 |
# ifdef A_DOT_OUT
|
|
132 |
# include <a.out.h>
|
|
133 |
# ifndef N_MAGIC
|
|
134 |
# if defined(sinix) && defined(BSD)
|
|
135 |
# define N_MAGIC(hdr) (hdr.a_magic & 0xFFFF)
|
|
136 |
# else
|
|
137 |
# define N_MAGIC(hdr) (hdr.a_magic)
|
|
138 |
# endif
|
|
139 |
# endif
|
|
140 |
# endif /* a.out */
|
|
141 |
|
|
142 |
# ifdef COFF
|
|
143 |
# ifdef mips
|
|
144 |
# include <sys/exec.h>
|
|
145 |
# else
|
|
146 |
# include <a.out.h>
|
|
147 |
# endif
|
|
148 |
# endif /* coff */
|
|
149 |
|
|
150 |
# ifdef ELF
|
|
151 |
# include <elf.h>
|
|
152 |
# endif /* elf */
|
|
153 |
|
|
154 |
#endif /* not HAS_DL */
|
|
155 |
|
95
|
156 |
#ifdef NEXT_DL
|
|
157 |
# ifndef _RLD_H_
|
|
158 |
# define _RLD_H_
|
|
159 |
# include <rld.h>
|
|
160 |
# endif
|
|
161 |
#endif /* NEXT_DL */
|
|
162 |
|
28
|
163 |
#ifdef SYSV4_DL
|
|
164 |
# include <dlfcn.h>
|
|
165 |
# define dlcfn_h
|
|
166 |
#endif
|
|
167 |
|
45
|
168 |
#ifdef GNU_DL
|
|
169 |
# ifndef dld_h
|
|
170 |
# include "dld.h"
|
|
171 |
# define dld_h
|
|
172 |
# endif
|
|
173 |
#endif
|
95
|
174 |
|
|
175 |
#ifdef DL1_6
|
|
176 |
# ifndef dl_h
|
|
177 |
# include "dl.h"
|
|
178 |
# define dl_h
|
|
179 |
# endif
|
|
180 |
#endif
|
|
181 |
|
97
|
182 |
#ifdef AIX_DL
|
|
183 |
# include <nlist.h>
|
100
|
184 |
# include <sys/ldr.h>
|
|
185 |
# include <errno.h>
|
97
|
186 |
#endif
|
0
|
187 |
%}
|
20
|
188 |
! !
|
|
189 |
|
0
|
190 |
!ObjectFileLoader class methodsFor:'initialization'!
|
|
191 |
|
|
192 |
initialize
|
|
193 |
"name of object file, where initial symbol table is found"
|
|
194 |
|
10
|
195 |
MySymbolTable := 'smalltalk'.
|
45
|
196 |
Verbose := false.
|
106
|
197 |
NextHandleID := 1.
|
107
|
198 |
ObjectMemory addDependent:self.
|
|
199 |
|
|
200 |
"
|
|
201 |
ObjectFileLoader initialize
|
|
202 |
"
|
0
|
203 |
!
|
|
204 |
|
|
205 |
verbose:aBoolean
|
|
206 |
"turn on/off debug traces"
|
|
207 |
|
10
|
208 |
Verbose := aBoolean
|
0
|
209 |
|
|
210 |
"ObjectFileLoader verbose:true"
|
|
211 |
! !
|
|
212 |
|
107
|
213 |
!ObjectFileLoader class methodsFor:'change & update'!
|
|
214 |
|
|
215 |
update:something
|
|
216 |
"sent, when image is saved or restarted"
|
|
217 |
|
|
218 |
(something == #aboutToSnapshot) ifTrue:[
|
|
219 |
self unloadAndRememberAllObjectFiles
|
|
220 |
].
|
|
221 |
(something == #restarted) ifTrue:[
|
|
222 |
self reloadAllRememberedObjectFiles
|
|
223 |
].
|
|
224 |
! !
|
|
225 |
|
0
|
226 |
!ObjectFileLoader class methodsFor:'command defaults'!
|
|
227 |
|
|
228 |
needSeparateIDSpaces
|
20
|
229 |
"return true, if we need separate I and D spaces.
|
|
230 |
This is only needed if no dynamic-link facilitiy exists."
|
0
|
231 |
|
|
232 |
|os cpu|
|
|
233 |
|
|
234 |
os := OperatingSystem getSystemType.
|
|
235 |
cpu := OperatingSystem getCPUType.
|
|
236 |
|
|
237 |
(os = 'sunos') ifTrue:[
|
45
|
238 |
(cpu = 'sparc') ifTrue:[ ^ true ]
|
0
|
239 |
].
|
6
|
240 |
(os = 'linux') ifTrue:[ ^ false ].
|
|
241 |
|
95
|
242 |
'OBJFLOADER: dont know if we need sepId - assume no' errorPrintNL.
|
6
|
243 |
^ false
|
0
|
244 |
!
|
|
245 |
|
|
246 |
absLd:file text:textAddr data:dataAddr
|
20
|
247 |
"this should return a string to link file.o to absolute address.
|
|
248 |
This is only needed if no dynamic-link facilitiy exists."
|
0
|
249 |
|
|
250 |
|os cpu|
|
|
251 |
|
|
252 |
os := OperatingSystem getSystemType.
|
|
253 |
cpu := OperatingSystem getCPUType.
|
|
254 |
(os = 'sunos') ifTrue:[
|
45
|
255 |
(cpu = 'sparc') ifTrue:[
|
6
|
256 |
" "
|
45
|
257 |
^ ('ld -A ' , MySymbolTable , ' -x -Bstatic' ,
|
|
258 |
' -Ttext ' , (textAddr printStringRadix:16) ,
|
|
259 |
' -Tdata ' , (dataAddr printStringRadix:16) , ' ' , file)
|
6
|
260 |
" "
|
0
|
261 |
"
|
45
|
262 |
^ ('ld -A ' , MySymbolTable , ' -T ',
|
|
263 |
(textAddr printStringRadix:16),
|
|
264 |
' -N -x ' , file)
|
6
|
265 |
"
|
45
|
266 |
]
|
0
|
267 |
].
|
|
268 |
"
|
|
269 |
(os = 'ultrix') ifTrue:[
|
45
|
270 |
(cpu = 'mips') ifTrue:[
|
|
271 |
^ ('ld -A ' , MySymbolTable , ' -x -N -T ' , (textAddr printStringRadix:16) , ' ' , file)
|
|
272 |
]
|
0
|
273 |
].
|
|
274 |
"
|
6
|
275 |
(os = 'linux') ifTrue:[
|
45
|
276 |
^ ('ld -A ' , MySymbolTable ,
|
|
277 |
' -x -N ',
|
|
278 |
' -Ttext ' , (textAddr printStringRadix:16) ,
|
|
279 |
' -Tdata ' , (dataAddr printStringRadix:16) , ' ' , file)
|
6
|
280 |
].
|
0
|
281 |
self error:'do not know how to link absolute'
|
6
|
282 |
!
|
|
283 |
|
|
284 |
absLd:file text:textAddr
|
20
|
285 |
"this should return a string to link file.o to absolute address.
|
|
286 |
This is only needed if no dynamic-link facilitiy exists."
|
6
|
287 |
|
|
288 |
|os cpu|
|
|
289 |
|
|
290 |
os := OperatingSystem getSystemType.
|
|
291 |
cpu := OperatingSystem getCPUType.
|
|
292 |
(os = 'sunos') ifTrue:[
|
45
|
293 |
(cpu = 'sparc') ifTrue:[
|
|
294 |
^ ('ld -A ' , MySymbolTable , ' -T ',
|
|
295 |
(textAddr printStringRadix:16),
|
|
296 |
' -N -x ' , file)
|
6
|
297 |
|
45
|
298 |
]
|
6
|
299 |
].
|
|
300 |
(os = 'linux') ifTrue:[
|
45
|
301 |
^ ('ld -A ' , MySymbolTable ,
|
|
302 |
' -x -N -Ttext ' , (textAddr printStringRadix:16) , ' ' , file)
|
6
|
303 |
].
|
|
304 |
self error:'do not know how to link absolute'
|
28
|
305 |
!
|
6
|
306 |
|
28
|
307 |
nm:file
|
|
308 |
"this should return a string to list the namelist of file"
|
|
309 |
|
95
|
310 |
|os|
|
28
|
311 |
|
|
312 |
os := OperatingSystem getSystemType.
|
|
313 |
(os = 'iris') ifTrue:[
|
45
|
314 |
^ 'nm -B ' , file
|
28
|
315 |
].
|
|
316 |
^ 'nm ' , file
|
0
|
317 |
! !
|
|
318 |
|
|
319 |
!ObjectFileLoader class methodsFor:'loading objects'!
|
|
320 |
|
|
321 |
loadFile:oFile with:librariesString
|
20
|
322 |
"load in an object files code, linking in libraries.
|
|
323 |
This is only needed if no dynamic link facility exists."
|
0
|
324 |
|
20
|
325 |
|tmpOfile errStream errors errText handle pid cmd|
|
0
|
326 |
|
|
327 |
pid := OperatingSystem getProcessId printString.
|
|
328 |
tmpOfile := '/tmp/stc_ld' , pid.
|
20
|
329 |
cmd := 'ld -o ', tmpOfile, ' -r ' , oFile , ' ' , librariesString , '>/tmp/out 2>/tmp/err'.
|
10
|
330 |
Verbose ifTrue:[
|
95
|
331 |
('executing: ld -o ', cmd) infoPrintNL
|
0
|
332 |
].
|
20
|
333 |
(OperatingSystem executeCommand:cmd) ifFalse:[
|
45
|
334 |
errStream := FileStream oldFileNamed:'/tmp/err'.
|
|
335 |
errStream isNil ifTrue:[
|
|
336 |
self notify:'errors during link.'
|
|
337 |
] ifFalse:[
|
|
338 |
errors := errStream contents.
|
58
|
339 |
errText := errors asStringCollection.
|
45
|
340 |
(errText size > 20) ifTrue:[
|
|
341 |
errText grow:20.
|
|
342 |
errText add:'... '.
|
|
343 |
errors := errText
|
|
344 |
].
|
|
345 |
OperatingSystem executeCommand:'rm /tmp/err /tmp/out'.
|
|
346 |
self notify:('link errors:\\' , errors asString) withCRs
|
|
347 |
].
|
|
348 |
^ false
|
0
|
349 |
].
|
20
|
350 |
handle := self loadFile:tmpOfile.
|
0
|
351 |
OperatingSystem executeCommand:('rm ' , tmpOfile).
|
20
|
352 |
^ handle
|
0
|
353 |
!
|
|
354 |
|
|
355 |
loadFile:oFile
|
20
|
356 |
"load in an object file - return a handle or nil.
|
45
|
357 |
This is only needed if no dynamic link facility exists.
|
|
358 |
It allocates some memory for text and data, calls for the linker
|
|
359 |
to relocate the oFile to that address and loads the sections."
|
0
|
360 |
|
20
|
361 |
|unixCommand errStream errors errText
|
|
362 |
text data textSize dataSize dataAddr textAddr newTextSize newDataSize|
|
0
|
363 |
|
|
364 |
"find out, how much memory we need"
|
|
365 |
|
20
|
366 |
textSize := self textSizeOf:oFile.
|
0
|
367 |
textSize isNil ifTrue:[
|
45
|
368 |
'bad text-size in object file' errorPrintNL.
|
|
369 |
^ nil
|
0
|
370 |
].
|
10
|
371 |
Verbose ifTrue:[
|
95
|
372 |
('text-size: ' , (textSize printStringRadix:16)) infoPrintNL
|
0
|
373 |
].
|
|
374 |
|
20
|
375 |
dataSize := self dataSizeOf:oFile.
|
0
|
376 |
dataSize isNil ifTrue:[
|
45
|
377 |
'bad data-size in object file' errorPrintNL.
|
|
378 |
^ nil
|
0
|
379 |
].
|
|
380 |
|
10
|
381 |
Verbose ifTrue:[
|
95
|
382 |
('data-size: ' , (dataSize printStringRadix:16)) infoPrintNL
|
0
|
383 |
].
|
|
384 |
|
|
385 |
"allocate some memory for text and some for data;
|
|
386 |
then call linker to link the file to those addresses"
|
|
387 |
|
|
388 |
self needSeparateIDSpaces ifTrue:[
|
45
|
389 |
text := ExternalBytes newForText:textSize.
|
|
390 |
text isNil ifTrue:[
|
|
391 |
'cannot allocate memory for text' errorPrintNL.
|
|
392 |
^ nil
|
|
393 |
].
|
0
|
394 |
|
45
|
395 |
Verbose ifTrue:[
|
95
|
396 |
('text: ' , (text address printStringRadix:16)) infoPrintNL
|
45
|
397 |
].
|
0
|
398 |
|
45
|
399 |
(dataSize ~~ 0) ifTrue:[
|
|
400 |
data := ExternalBytes newForData:dataSize.
|
|
401 |
(data isNil) ifTrue:[
|
|
402 |
'cannot allocate memory for data' errorPrintNL.
|
|
403 |
text notNil ifTrue:[text free].
|
|
404 |
^ nil
|
|
405 |
].
|
|
406 |
Verbose ifTrue:[
|
95
|
407 |
('data: ' , (data address printStringRadix:16)) infoPrintNL
|
45
|
408 |
]
|
|
409 |
].
|
|
410 |
dataSize == 0 ifTrue:[
|
|
411 |
unixCommand := (self absLd:oFile text:text address) , ' >/tmp/out 2>/tmp/err'.
|
|
412 |
] ifFalse:[
|
|
413 |
unixCommand := (self absLd:oFile text:text address data:data address)
|
|
414 |
, ' >/tmp/out 2>/tmp/err'.
|
|
415 |
]
|
0
|
416 |
] ifFalse:[
|
45
|
417 |
text := ExternalBytes newForText:(textSize + dataSize).
|
|
418 |
text isNil ifTrue:[
|
|
419 |
'cannot allocate memory for text+data' errorPrintNL.
|
|
420 |
^ nil
|
|
421 |
].
|
|
422 |
Verbose ifTrue:[
|
95
|
423 |
('addr: ' , (text address printStringRadix:16)) infoPrintNL
|
45
|
424 |
].
|
|
425 |
unixCommand := (self absLd:oFile text:text address) , ' >/tmp/out 2>/tmp/err'.
|
0
|
426 |
].
|
|
427 |
|
10
|
428 |
Verbose ifTrue:[
|
95
|
429 |
('executing: ' , unixCommand) infoPrintNL
|
0
|
430 |
].
|
|
431 |
|
19
|
432 |
'linking ...' printNewline.
|
0
|
433 |
(OperatingSystem executeCommand:unixCommand) ifFalse: [
|
45
|
434 |
errStream := FileStream oldFileNamed:'/tmp/err'.
|
|
435 |
errStream notNil ifTrue:[
|
|
436 |
errors := errStream contents.
|
58
|
437 |
errText := errors asStringCollection.
|
45
|
438 |
(errText size > 20) ifTrue:[
|
|
439 |
errText grow:20.
|
|
440 |
errText add:'... '.
|
|
441 |
errors := errText
|
|
442 |
].
|
|
443 |
OperatingSystem executeCommand:'rm /tmp/err /tmp/out'.
|
|
444 |
self notify:('link errors:\\' , errors asString) withCRs
|
|
445 |
].
|
|
446 |
'link unsuccessful.' errorPrintNL.
|
|
447 |
text notNil ifTrue:[text free].
|
|
448 |
data notNil ifTrue:[data free].
|
|
449 |
^ nil
|
0
|
450 |
].
|
|
451 |
|
95
|
452 |
'link successful' infoPrintNL.
|
0
|
453 |
|
|
454 |
OperatingSystem executeCommand:'rm /tmp/err /tmp/out'.
|
|
455 |
|
6
|
456 |
"find out, if space needs have changed after link (they do so on some machines)"
|
|
457 |
|
20
|
458 |
newTextSize := self textSizeOf:'a.out'.
|
6
|
459 |
newTextSize isNil ifTrue:[
|
45
|
460 |
'bad new-text-size in a.out object file' errorPrintNL.
|
|
461 |
text notNil ifTrue:[text free].
|
|
462 |
data notNil ifTrue:[data free].
|
|
463 |
^ nil
|
6
|
464 |
].
|
10
|
465 |
Verbose ifTrue:[
|
95
|
466 |
('new-text-size: ' , (newTextSize printStringRadix:16)) infoPrintNL
|
6
|
467 |
].
|
|
468 |
|
20
|
469 |
newDataSize := self dataSizeOf:'a.out'.
|
6
|
470 |
newDataSize isNil ifTrue:[
|
45
|
471 |
'bad new-data-size in a.out object file' errorPrintNL.
|
|
472 |
text notNil ifTrue:[text free].
|
|
473 |
data notNil ifTrue:[data free].
|
|
474 |
^ nil
|
6
|
475 |
].
|
|
476 |
|
10
|
477 |
Verbose ifTrue:[
|
95
|
478 |
('new-data-size: ' , (newDataSize printStringRadix:16)) infoPrintNL
|
6
|
479 |
].
|
|
480 |
|
|
481 |
"if size has changed, do it again"
|
|
482 |
|
|
483 |
((newTextSize ~~ textSize) or:[newDataSize ~~ dataSize]) ifTrue:[
|
|
484 |
|
45
|
485 |
'size changed after link - do it again' printNewline.
|
6
|
486 |
|
45
|
487 |
text notNil ifTrue:[text free. text := nil].
|
|
488 |
data notNil ifTrue:[data free. data := nil].
|
|
489 |
textSize := newTextSize.
|
|
490 |
dataSize := newDataSize.
|
6
|
491 |
|
45
|
492 |
self needSeparateIDSpaces ifTrue:[
|
|
493 |
text := ExternalBytes newForText:textSize.
|
|
494 |
text isNil ifTrue:[
|
|
495 |
'cannot allocate memory for new text' errorPrintNL.
|
|
496 |
^ nil
|
|
497 |
].
|
6
|
498 |
|
45
|
499 |
Verbose ifTrue:[
|
95
|
500 |
('new text: ' , (text address printStringRadix:16)) infoPrintNL
|
45
|
501 |
].
|
6
|
502 |
|
45
|
503 |
(dataSize ~~ 0) ifTrue:[
|
|
504 |
data := ExternalBytes newForData:dataSize.
|
|
505 |
(data isNil) ifTrue:[
|
|
506 |
'cannot allocate memory for new data' errorPrintNL.
|
|
507 |
text notNil ifTrue:[text free].
|
|
508 |
^ nil
|
|
509 |
].
|
|
510 |
Verbose ifTrue:[
|
95
|
511 |
('new data: ' , (data address printStringRadix:16)) infoPrintNL
|
45
|
512 |
]
|
|
513 |
].
|
6
|
514 |
|
45
|
515 |
dataSize == 0 ifTrue:[
|
|
516 |
unixCommand := (self absLd:oFile text:text address) , ' >/tmp/out 2>/tmp/err'.
|
|
517 |
] ifFalse:[
|
|
518 |
unixCommand := (self absLd:oFile text:text address data:data address)
|
|
519 |
, ' >/tmp/out 2>/tmp/err'.
|
|
520 |
]
|
|
521 |
] ifFalse:[
|
|
522 |
text := ExternalBytes newForText:(textSize + dataSize).
|
|
523 |
text isNil ifTrue:[
|
|
524 |
'cannot allocate memory for new text' errorPrintNL.
|
|
525 |
^ nil
|
|
526 |
].
|
|
527 |
Verbose ifTrue:[
|
95
|
528 |
('new text+data: ' , (text address printStringRadix:16)) infoPrintNL
|
45
|
529 |
].
|
|
530 |
unixCommand := (self absLd:oFile text:text address) , ' >/tmp/out 2>/tmp/err'.
|
|
531 |
].
|
6
|
532 |
|
45
|
533 |
Verbose ifTrue:[
|
95
|
534 |
('executing: ' , unixCommand) infoPrintNL
|
45
|
535 |
].
|
6
|
536 |
|
95
|
537 |
'linking ...' infoPrintNL.
|
45
|
538 |
(OperatingSystem executeCommand:unixCommand) ifFalse: [
|
|
539 |
errStream := FileStream oldFileNamed:'/tmp/err'.
|
|
540 |
errStream notNil ifTrue:[
|
|
541 |
errors := errStream contents.
|
58
|
542 |
errText := errors asStringCollection.
|
45
|
543 |
(errText size > 20) ifTrue:[
|
|
544 |
errText grow:20.
|
|
545 |
errText add:'... '.
|
|
546 |
errors := errText
|
|
547 |
].
|
|
548 |
OperatingSystem executeCommand:'rm /tmp/err /tmp/out'.
|
|
549 |
self notify:('link errors:\\' , errors asString) withCRs
|
|
550 |
].
|
|
551 |
'link unsuccessful.' errorPrintNL.
|
|
552 |
text notNil ifTrue:[text free].
|
|
553 |
data notNil ifTrue:[data free].
|
|
554 |
^ nil
|
|
555 |
].
|
6
|
556 |
|
95
|
557 |
'link successful' infoPrintNL.
|
6
|
558 |
|
45
|
559 |
OperatingSystem executeCommand:'rm /tmp/err /tmp/out'.
|
6
|
560 |
|
45
|
561 |
"check again for size change - should not happen"
|
6
|
562 |
|
45
|
563 |
newTextSize := self textSizeOf:'a.out'.
|
|
564 |
newTextSize isNil ifTrue:[
|
|
565 |
'bad text-size in a.out object file' errorPrintNL.
|
|
566 |
text notNil ifTrue:[text free].
|
|
567 |
data notNil ifTrue:[data free].
|
|
568 |
^ nil
|
|
569 |
].
|
|
570 |
Verbose ifTrue:[
|
95
|
571 |
('new-text-size: ' , (newTextSize printStringRadix:16)) infoPrintNL
|
45
|
572 |
].
|
6
|
573 |
|
45
|
574 |
newDataSize := self dataSizeOf:'a.out'.
|
|
575 |
newDataSize isNil ifTrue:[
|
|
576 |
'bad data-size in object file' errorPrintNL.
|
|
577 |
text notNil ifTrue:[text free].
|
|
578 |
data notNil ifTrue:[data free].
|
|
579 |
^ nil
|
|
580 |
].
|
6
|
581 |
|
45
|
582 |
Verbose ifTrue:[
|
95
|
583 |
('new-data-size: ' , (newDataSize printStringRadix:16)) infoPrintNL
|
45
|
584 |
].
|
6
|
585 |
|
45
|
586 |
((newTextSize ~~ textSize) or:[newDataSize ~~ dataSize]) ifTrue:[
|
|
587 |
'size changed again - I give up' errorPrintNL.
|
|
588 |
text notNil ifTrue:[text free].
|
|
589 |
data notNil ifTrue:[data free].
|
|
590 |
^ nil
|
|
591 |
].
|
6
|
592 |
].
|
|
593 |
|
0
|
594 |
"only thing left to do is to load in text at textAddr and
|
|
595 |
data at dataAddr ... "
|
|
596 |
|
6
|
597 |
text notNil ifTrue:[
|
45
|
598 |
textAddr := text address
|
6
|
599 |
] ifFalse:[
|
45
|
600 |
textAddr := nil
|
6
|
601 |
].
|
|
602 |
data notNil ifTrue:[
|
45
|
603 |
dataAddr := data address
|
6
|
604 |
] ifFalse:[
|
45
|
605 |
dataAddr := nil
|
6
|
606 |
].
|
|
607 |
|
19
|
608 |
Verbose ifTrue:[
|
45
|
609 |
textAddr notNil ifTrue:[
|
95
|
610 |
('loading ' , textSize printString , ' bytes at ' , (textAddr printStringRadix:16)) infoPrintNL.
|
45
|
611 |
].
|
|
612 |
dataAddr notNil ifTrue:[
|
95
|
613 |
('loading ' , dataSize printString , ' bytes at ' , (dataAddr printStringRadix:16)) infoPrintNL.
|
45
|
614 |
].
|
19
|
615 |
].
|
6
|
616 |
|
20
|
617 |
(self loadObjectFile:'a.out'
|
45
|
618 |
textAddr:textAddr textSize:textSize
|
|
619 |
dataAddr:dataAddr dataSize:dataSize) isNil ifTrue: [
|
|
620 |
'load error' errorPrintNL.
|
|
621 |
text notNil ifTrue:[text free].
|
|
622 |
data notNil ifTrue:[data free].
|
|
623 |
^ nil
|
0
|
624 |
].
|
|
625 |
|
95
|
626 |
'dynamic load successful' infoPrintNL.
|
0
|
627 |
|
|
628 |
OperatingSystem executeCommand:'mv a.out SymbolTable'.
|
10
|
629 |
MySymbolTable := 'SymbolTable'.
|
45
|
630 |
^ (Array with:text with:data)
|
0
|
631 |
! !
|
|
632 |
|
|
633 |
!ObjectFileLoader class methodsFor:'dynamic class loading'!
|
|
634 |
|
|
635 |
loadClass:aClassName fromObjectFile:aFileName
|
|
636 |
"load a compiled class (.o-file) into the image"
|
|
637 |
|
110
|
638 |
|handle initAddr symName newClass list moreHandles status otherClass knownToBeOk|
|
0
|
639 |
|
106
|
640 |
handle := self loadDynamicObject:aFileName.
|
0
|
641 |
handle isNil ifTrue:[
|
106
|
642 |
Transcript showCr:('loadDynamic: ', aFileName,' failed.').
|
45
|
643 |
^ nil
|
0
|
644 |
].
|
20
|
645 |
|
|
646 |
"
|
|
647 |
get the Init-function; let the class install itself
|
|
648 |
"
|
19
|
649 |
symName := '_' , aClassName , '_Init'.
|
20
|
650 |
initAddr := self getFunction:symName from:handle.
|
0
|
651 |
initAddr isNil ifTrue:[
|
45
|
652 |
"try with added underscore"
|
|
653 |
symName := '__' , aClassName , '_Init'.
|
|
654 |
initAddr := self getFunction:symName from:handle.
|
20
|
655 |
].
|
|
656 |
|
110
|
657 |
knownToBeOk := true.
|
|
658 |
knownToBeOk ifFalse:[
|
111
|
659 |
Verbose ifTrue:[
|
110
|
660 |
'looking for undefs ...' infoPrintNL.
|
111
|
661 |
].
|
95
|
662 |
|
111
|
663 |
"
|
|
664 |
if there are any undefined symbols, we may have to load more
|
|
665 |
files (libs, other modules)
|
|
666 |
"
|
|
667 |
list := self getListOfUndefinedSymbolsFrom:handle.
|
|
668 |
list notNil ifTrue:[
|
110
|
669 |
moreHandles := self loadModulesFromListOfUndefined:list.
|
20
|
670 |
|
110
|
671 |
"
|
|
672 |
now, try again
|
|
673 |
"
|
|
674 |
symName := '_' , aClassName , '_Init'.
|
45
|
675 |
initAddr := self getFunction:symName from:handle.
|
110
|
676 |
initAddr isNil ifTrue:[
|
111
|
677 |
"try with added underscore"
|
|
678 |
symName := '__' , aClassName , '_Init'.
|
|
679 |
initAddr := self getFunction:symName from:handle.
|
110
|
680 |
].
|
|
681 |
]
|
0
|
682 |
].
|
20
|
683 |
|
|
684 |
initAddr notNil ifTrue:[
|
45
|
685 |
Verbose ifTrue:[
|
107
|
686 |
('calling init at: ' , (initAddr printStringRadix:16)) infoPrintNL.
|
45
|
687 |
].
|
106
|
688 |
status := self performModuleInitAt:initAddr identifyAs:handle.
|
95
|
689 |
"
|
|
690 |
if any classes are missing ...
|
|
691 |
"
|
|
692 |
((status at:1) == #missingClass:) ifTrue:[
|
|
693 |
"
|
|
694 |
... and we are loading a module ...
|
|
695 |
"
|
|
696 |
Transcript showCr:'try for missing class in same object ...'.
|
|
697 |
otherClass := self loadClass:(status at:2) fromObjectFile:aFileName.
|
|
698 |
otherClass notNil ifTrue:[
|
|
699 |
"
|
|
700 |
try again ...
|
|
701 |
"
|
|
702 |
Transcript showCr:'missing class is here; try again ...'.
|
106
|
703 |
status := self performModuleInitAt:initAddr identifyAs:handle.
|
95
|
704 |
]
|
|
705 |
].
|
|
706 |
|
45
|
707 |
(Symbol hasInterned:aClassName) ifTrue:[
|
|
708 |
newClass := Smalltalk at:aClassName asSymbol ifAbsent:[nil].
|
|
709 |
newClass notNil ifTrue:[
|
|
710 |
newClass initialize.
|
|
711 |
"force cache flush"
|
|
712 |
Smalltalk at:aClassName asSymbol put:newClass.
|
|
713 |
Smalltalk changed.
|
|
714 |
].
|
|
715 |
] ifFalse:[
|
95
|
716 |
'LOADER: class ' errorPrint. aClassName errorPrint.
|
45
|
717 |
' did not define itself' errorPrintNL
|
|
718 |
"
|
|
719 |
do not unload - could have installed its methods ...
|
|
720 |
"
|
|
721 |
].
|
|
722 |
^ newClass
|
20
|
723 |
].
|
|
724 |
|
|
725 |
Verbose ifTrue:[
|
107
|
726 |
('no symbol: ', symName,' in ',aFileName) infoPrintNL.
|
20
|
727 |
].
|
|
728 |
|
|
729 |
"
|
|
730 |
unload
|
|
731 |
"
|
110
|
732 |
Verbose ifTrue:[
|
111
|
733 |
'unloading due to init failure:' infoPrint. (handle at:3) infoPrintNL.
|
110
|
734 |
].
|
|
735 |
|
20
|
736 |
moreHandles notNil ifTrue:[
|
95
|
737 |
moreHandles do:[:aHandle |
|
|
738 |
Verbose ifTrue:[
|
107
|
739 |
('unloading: ', aHandle printString) infoPrintNL.
|
95
|
740 |
].
|
107
|
741 |
self unloadDynamicObject:handle.
|
95
|
742 |
]
|
|
743 |
].
|
|
744 |
|
|
745 |
Verbose ifTrue:[
|
107
|
746 |
('unloading: ', handle printString) infoPrintNL.
|
20
|
747 |
].
|
107
|
748 |
self unloadDynamicObject:handle.
|
20
|
749 |
^ nil
|
0
|
750 |
|
95
|
751 |
"
|
|
752 |
ObjectFileLoader loadClass:'Tetris' fromObjectFile:'../clients/Tetris/Tetris.o'
|
|
753 |
ObjectFileLoader loadClass:'TetrisBlock' fromObjectFile:'../clients/Tetris/TBlock.o'
|
|
754 |
ObjectFileLoader loadClass:'Foo' fromObjectFile:'classList.o'
|
|
755 |
"
|
0
|
756 |
!
|
|
757 |
|
|
758 |
loadObjectFile:aFileName
|
20
|
759 |
"load an object file (.o-file) into the image;
|
37
|
760 |
the class name is not needed (multiple definitions may be in the file).
|
|
761 |
Return false on error, true if ok."
|
0
|
762 |
|
96
|
763 |
|handle initAddr className newClass initNames didInit status suffixLen|
|
0
|
764 |
|
106
|
765 |
handle := self loadDynamicObject:aFileName.
|
0
|
766 |
handle isNil ifTrue:[
|
106
|
767 |
Transcript showCr:('loadDynamic: ',aFileName,' failed.').
|
45
|
768 |
^ false
|
0
|
769 |
].
|
19
|
770 |
|
51
|
771 |
didInit := false.
|
|
772 |
|
95
|
773 |
"
|
|
774 |
first, expect the classes-name to be the fileName-base
|
|
775 |
"
|
0
|
776 |
className := OperatingSystem baseNameOf:aFileName.
|
|
777 |
(className endsWith:'.o') ifTrue:[
|
96
|
778 |
suffixLen := 2.
|
37
|
779 |
] ifFalse:[
|
45
|
780 |
(className endsWith:'.obj') ifTrue:[
|
96
|
781 |
suffixLen := 4.
|
45
|
782 |
] ifFalse:[
|
|
783 |
(className endsWith:'.so') ifTrue:[
|
96
|
784 |
suffixLen := 3.
|
|
785 |
] ifFalse:[
|
|
786 |
suffixLen := 0.
|
45
|
787 |
]
|
|
788 |
]
|
0
|
789 |
].
|
96
|
790 |
suffixLen ~~ 0 ifTrue:[
|
|
791 |
className := className copyWithoutLast:suffixLen
|
|
792 |
].
|
19
|
793 |
|
28
|
794 |
"
|
37
|
795 |
look for explicit init function
|
28
|
796 |
"
|
37
|
797 |
initAddr := self getFunction:(className , '_Init') from:handle.
|
|
798 |
initAddr isNil ifTrue:[
|
45
|
799 |
initAddr := self getFunction:('_' , className , '_Init') from:handle.
|
37
|
800 |
].
|
51
|
801 |
initAddr isNil ifTrue:[
|
|
802 |
"
|
|
803 |
look for reverse abbreviation
|
|
804 |
"
|
|
805 |
className := Smalltalk classNameForFile:className.
|
|
806 |
className notNil ifTrue:[
|
|
807 |
initAddr := self getFunction:(className , '_Init') from:handle.
|
|
808 |
initAddr isNil ifTrue:[
|
|
809 |
initAddr := self getFunction:('_' , className , '_Init') from:handle.
|
|
810 |
].
|
|
811 |
]
|
|
812 |
].
|
37
|
813 |
initAddr notNil ifTrue:[
|
51
|
814 |
Verbose ifTrue:[
|
107
|
815 |
('calling init at:' , (initAddr printStringRadix:16)) infoPrintNL.
|
51
|
816 |
].
|
106
|
817 |
status := self performModuleInitAt:initAddr identifyAs:handle.
|
51
|
818 |
didInit := true.
|
37
|
819 |
] ifFalse:[
|
45
|
820 |
"
|
95
|
821 |
look for init-function(s); call them all
|
45
|
822 |
"
|
95
|
823 |
Verbose ifTrue:[
|
107
|
824 |
'no init found; looking for candidates ...' infoPrintNL.
|
95
|
825 |
].
|
45
|
826 |
initNames := self namesMatching:'*_Init' segment:'[tT]' in:aFileName.
|
100
|
827 |
initNames notNil ifTrue:[
|
|
828 |
initNames do:[:aName |
|
|
829 |
initAddr := self getFunction:aName from:handle.
|
|
830 |
initAddr isNil ifTrue:[
|
|
831 |
(aName startsWith:'_') ifTrue:[
|
|
832 |
initAddr := self getFunction:(aName copyFrom:2) from:handle.
|
|
833 |
].
|
45
|
834 |
].
|
100
|
835 |
initAddr isNil ifTrue:[
|
|
836 |
Transcript showCr:('no symbol: ',aName,' in ',aFileName).
|
|
837 |
^ false
|
|
838 |
].
|
|
839 |
Verbose ifTrue:[
|
107
|
840 |
('calling init at:' , (initAddr printStringRadix:16)) infoPrintNL
|
100
|
841 |
].
|
106
|
842 |
self performModuleInitAt:initAddr identifyAs:handle.
|
100
|
843 |
didInit := true.
|
45
|
844 |
].
|
|
845 |
].
|
0
|
846 |
].
|
20
|
847 |
|
37
|
848 |
"/ (Symbol hasInterned:className) ifTrue:[
|
|
849 |
"/ newClass := Smalltalk at:className asSymbol ifAbsent:[nil].
|
|
850 |
"/ newClass notNil ifTrue:[
|
|
851 |
"/ newClass initialize.
|
|
852 |
"/ "force cache flush"
|
|
853 |
"/ Smalltalk at:className asSymbol put:newClass.
|
|
854 |
"/ ].
|
|
855 |
"/ ].
|
|
856 |
|
51
|
857 |
didInit ifFalse:[
|
|
858 |
Transcript showCr:'no init function found'
|
|
859 |
].
|
|
860 |
|
37
|
861 |
"
|
|
862 |
really dont know, if it has changed ...
|
|
863 |
"
|
|
864 |
Smalltalk changed.
|
|
865 |
^ true
|
20
|
866 |
!
|
|
867 |
|
|
868 |
loadCPlusPlusObjectFile:aFileName
|
|
869 |
"load a c++ object file (.o-file) into the image"
|
|
870 |
|
95
|
871 |
|handle initAddr list|
|
20
|
872 |
|
106
|
873 |
handle := self loadDynamicObject:aFileName.
|
20
|
874 |
handle isNil ifTrue:[
|
106
|
875 |
Transcript showCr:('loadDynamic: ',aFileName,' failed.').
|
45
|
876 |
^ nil
|
20
|
877 |
].
|
|
878 |
|
28
|
879 |
list := self namesMatching:'__GLOBAL_$I*' segment:'[tT]' in:aFileName.
|
20
|
880 |
list size == 1 ifTrue:[
|
|
881 |
"/ (self isCPlusPlusObject:handle) ifTrue:[
|
45
|
882 |
Verbose ifTrue:[
|
107
|
883 |
'a c++ object file' infoPrintNL.
|
45
|
884 |
].
|
|
885 |
"
|
|
886 |
what I would like to get is the CTOR_LIST,
|
|
887 |
and call each function.
|
|
888 |
But dld cannot (currently) handle SET-type symbols, therefore
|
|
889 |
we search (using nm) for all __GLOBAL_$I* syms, get their values
|
|
890 |
and call them each
|
|
891 |
"
|
28
|
892 |
"/ list := self namesMatching:'__GLOBAL_$I*' segment:'[tT]' in:aFileName.
|
20
|
893 |
|
|
894 |
"/ initAddr := self getFunction:'__CTOR_LIST__' from:handle.
|
|
895 |
"/ Verbose ifTrue:[
|
107
|
896 |
"/ ('calling CTORs at:' , (initAddr printStringRadix:16)) infoPrintNL
|
20
|
897 |
"/ ].
|
|
898 |
|
45
|
899 |
initAddr := self getFunction:list first from:handle.
|
|
900 |
initAddr isNil ifTrue:[
|
|
901 |
"
|
|
902 |
try with added underscore
|
|
903 |
"
|
|
904 |
initAddr := self getFunction:('_' , list first) from:handle.
|
|
905 |
].
|
|
906 |
(initAddr isNil and:[list first startsWith:'_']) ifTrue:[
|
|
907 |
"
|
|
908 |
try with removed underscore
|
|
909 |
"
|
|
910 |
initAddr := self getFunction:(list first copyFrom:2) from:handle.
|
|
911 |
].
|
|
912 |
initAddr isNil ifTrue:[
|
|
913 |
Verbose ifTrue:[
|
107
|
914 |
('no CTOR-func found (' , list first , ')') infoPrintNL.
|
45
|
915 |
].
|
107
|
916 |
self unloadDynamicObject:aFileName.
|
45
|
917 |
^ nil
|
|
918 |
].
|
|
919 |
Verbose ifTrue:[
|
107
|
920 |
('calling CTORs at:' , (initAddr printStringRadix:16)) infoPrintNL
|
45
|
921 |
].
|
82
|
922 |
self callInitFunctionAt:initAddr
|
|
923 |
specialInit:false
|
|
924 |
forceOld:false
|
|
925 |
interruptable:false
|
106
|
926 |
argument:0
|
|
927 |
identifyAs:nil.
|
82
|
928 |
|
45
|
929 |
Verbose ifTrue:[
|
107
|
930 |
'done with CTORs.' infoPrintNL
|
45
|
931 |
].
|
20
|
932 |
|
45
|
933 |
"
|
|
934 |
cannot create a CPlusPlus class automatically (there could be more than
|
|
935 |
one classes in it too ...)
|
|
936 |
"
|
|
937 |
^ handle
|
20
|
938 |
].
|
|
939 |
|
|
940 |
|
|
941 |
Verbose ifTrue:[
|
107
|
942 |
'unknown object file' infoPrintNL
|
20
|
943 |
].
|
107
|
944 |
self unloadDynamicObject:aFileName.
|
20
|
945 |
^ nil
|
|
946 |
!
|
|
947 |
|
|
948 |
loadModulesFromListOfUndefined:list
|
|
949 |
"try to figure out what has to be loaded to resolve symbols from list.
|
|
950 |
return a list of handles of loaded objects
|
|
951 |
"
|
95
|
952 |
|inits classNames|
|
20
|
953 |
|
|
954 |
inits := list select:[:symbol | symbol notNil and:[symbol endsWith:'_Init']].
|
|
955 |
inits notNil ifTrue:[
|
45
|
956 |
classNames := inits collect:[:symbol |
|
|
957 |
(symbol startsWith:'___') ifTrue:[
|
|
958 |
symbol copyFrom:4 to:(symbol size - 5)
|
|
959 |
] ifFalse:[
|
|
960 |
(symbol startsWith:'__') ifTrue:[
|
|
961 |
symbol copyFrom:3 to:(symbol size - 5)
|
|
962 |
] ifFalse:[
|
|
963 |
(symbol startsWith:'_') ifTrue:[
|
|
964 |
symbol copyFrom:2 to:(symbol size - 5)
|
|
965 |
] ifFalse:[
|
|
966 |
symbol
|
|
967 |
]
|
|
968 |
]
|
|
969 |
]
|
|
970 |
].
|
|
971 |
"
|
|
972 |
autoload those classes
|
|
973 |
"
|
|
974 |
classNames do:[:aClassName |
|
|
975 |
|cls|
|
|
976 |
|
|
977 |
(cls := Smalltalk classNamed:aClassName) notNil ifTrue:[
|
|
978 |
'autoloading ' print. aClassName printNL.
|
|
979 |
cls autoload
|
|
980 |
]
|
|
981 |
]
|
20
|
982 |
].
|
|
983 |
^ nil
|
45
|
984 |
!
|
|
985 |
|
|
986 |
unloadObjectFile:aFileName
|
|
987 |
"unload an object file (.o-file) from the image.
|
|
988 |
DANGER ALERT: currently, you have to make sure that no references to
|
|
989 |
objects of this module exist - in future versions, the system will keep
|
|
990 |
track of these. For now, use at your own risk.
|
|
991 |
(especially critical are blocks-functions)."
|
|
992 |
|
|
993 |
|handle|
|
|
994 |
|
|
995 |
LoadedObjects notNil ifTrue:[
|
|
996 |
handle := LoadedObjects at:aFileName
|
|
997 |
].
|
|
998 |
handle isNil ifTrue:[
|
|
999 |
self error:'this file was not loaded dynamically'.
|
|
1000 |
^ self
|
|
1001 |
].
|
|
1002 |
|
89
|
1003 |
"/ call the modules deInit-function ...
|
45
|
1004 |
|
89
|
1005 |
"/ unload ...
|
|
1006 |
|
107
|
1007 |
self unloadDynamicObject:handle
|
45
|
1008 |
!
|
|
1009 |
|
|
1010 |
unloadAllObjectFiles
|
|
1011 |
"unload all dynamically loaded object files from the image.
|
|
1012 |
see DANGER ALERT in ObjectFileLoader>>unloadObjectFile:"
|
|
1013 |
|
|
1014 |
LoadedObjects notNil ifTrue:[
|
107
|
1015 |
LoadedObjects keys copy do:[:aFileName |
|
45
|
1016 |
self unloadObjectFile:aFileName
|
|
1017 |
]
|
|
1018 |
].
|
|
1019 |
|
|
1020 |
"
|
|
1021 |
ObjectFileLoader unloadAllObjectFiles
|
|
1022 |
"
|
107
|
1023 |
!
|
|
1024 |
|
|
1025 |
unloadAndRememberAllObjectFiles
|
108
|
1026 |
LoadedObjects notNil ifTrue:[
|
|
1027 |
PreviouslyLoadedObjects := LoadedObjects keys copy.
|
|
1028 |
self unloadAllObjectFiles
|
|
1029 |
]
|
107
|
1030 |
!
|
|
1031 |
|
|
1032 |
reloadAllRememberedObjectFiles
|
108
|
1033 |
PreviouslyLoadedObjects notNil ifTrue:[
|
|
1034 |
PreviouslyLoadedObjects do:[:fileName |
|
112
|
1035 |
('OBJFLOADER: reloading ' , fileName , ' ...') infoPrintNL.
|
108
|
1036 |
self loadObjectFile:fileName
|
|
1037 |
].
|
|
1038 |
PreviouslyLoadedObjects := nil
|
|
1039 |
]
|
107
|
1040 |
! !
|
|
1041 |
|
|
1042 |
!ObjectFileLoader class methodsFor:'queries'!
|
|
1043 |
|
|
1044 |
loadedObjectFiles
|
|
1045 |
"return a collection containing the names of all
|
|
1046 |
dynamically loaded objects."
|
|
1047 |
|
111
|
1048 |
LoadedObjects isNil ifTrue:[^ #()].
|
107
|
1049 |
^ LoadedObjects keys copy
|
|
1050 |
|
|
1051 |
"
|
|
1052 |
ObjectFileLoader loadedObjectFiles
|
|
1053 |
"
|
0
|
1054 |
! !
|
|
1055 |
|
|
1056 |
!ObjectFileLoader class methodsFor:'dynamic object access'!
|
|
1057 |
|
106
|
1058 |
loadDynamicObject:pathName
|
|
1059 |
"load an object-file (load/map into my address space).
|
0
|
1060 |
Return a non-nil handle if ok, nil otherwise.
|
20
|
1061 |
No bindings are done - only a pure load is performed.
|
|
1062 |
This function is not supported on all architectures.
|
|
1063 |
"
|
0
|
1064 |
|
106
|
1065 |
|handle buffer|
|
6
|
1066 |
|
20
|
1067 |
Verbose ifTrue:[
|
107
|
1068 |
('loadDynamic: ' , pathName , ' ...') infoPrintNL
|
20
|
1069 |
].
|
|
1070 |
|
94
|
1071 |
"/ already loaded ?
|
|
1072 |
|
|
1073 |
LoadedObjects notNil ifTrue:[
|
|
1074 |
handle := LoadedObjects at:pathName ifAbsent:nil.
|
|
1075 |
handle notNil ifTrue:[
|
|
1076 |
Verbose ifTrue:[
|
107
|
1077 |
('... ' , pathName , ' already loaded.') infoPrintNL.
|
94
|
1078 |
].
|
|
1079 |
^ handle
|
|
1080 |
].
|
|
1081 |
].
|
|
1082 |
|
106
|
1083 |
"/
|
|
1084 |
"/ the 1st two entries are system dependent;
|
|
1085 |
"/ entry 3 is the pathName
|
|
1086 |
"/ entry 4 is a unique ID
|
|
1087 |
"/
|
|
1088 |
buffer := Array new:4.
|
|
1089 |
buffer at:3 put:pathName.
|
|
1090 |
buffer at:4 put:NextHandleID. NextHandleID := NextHandleID + 1.
|
|
1091 |
|
|
1092 |
handle := self primLoadDynamicObject:pathName into:buffer.
|
20
|
1093 |
handle isNil ifTrue:[
|
45
|
1094 |
LastError == #notImplemented ifTrue:[
|
|
1095 |
Verbose ifTrue:[
|
107
|
1096 |
'no dynamic load facility or load failed.' infoPrintNL.
|
95
|
1097 |
Transcript showCr:'Try to load it myself ...'.
|
45
|
1098 |
].
|
|
1099 |
"try it the hard way"
|
|
1100 |
handle := self loadFile:pathName.
|
106
|
1101 |
].
|
|
1102 |
handle isNil ifTrue:[
|
|
1103 |
^ nil
|
45
|
1104 |
]
|
20
|
1105 |
].
|
45
|
1106 |
|
|
1107 |
"
|
|
1108 |
remember loaded object for later unloading
|
|
1109 |
"
|
|
1110 |
LoadedObjects isNil ifTrue:[
|
|
1111 |
LoadedObjects := Dictionary new.
|
|
1112 |
].
|
|
1113 |
LoadedObjects at:pathName put:handle.
|
93
|
1114 |
Smalltalk flushCachedClasses.
|
45
|
1115 |
|
95
|
1116 |
Verbose ifTrue:[
|
107
|
1117 |
('loadDynamic ok; handle is: ' , handle printString) infoPrintNL.
|
95
|
1118 |
].
|
|
1119 |
|
15
|
1120 |
^ handle
|
0
|
1121 |
|
|
1122 |
"sys5.4:
|
|
1123 |
|handle|
|
106
|
1124 |
handle := ObjectFileLoader loadDynamicObject:'../stc/mod1.so'.
|
20
|
1125 |
ObjectFileLoader getFunction:'module1' from:handle
|
15
|
1126 |
"
|
0
|
1127 |
"next:
|
|
1128 |
|handle|
|
106
|
1129 |
handle := ObjectFileLoader loadDynamicObject:'../goodies/Path/AbstrPath.o'.
|
20
|
1130 |
ObjectFileLoader getFunction:'__AbstractPath_Init' from:handle
|
15
|
1131 |
"
|
|
1132 |
"GLD:
|
|
1133 |
|handle|
|
106
|
1134 |
handle := ObjectFileLoader loadDynamicObject:'../clients/Tetris/Tetris.o'.
|
20
|
1135 |
ObjectFileLoader getFunction:'__TetrisBlock_Init' from:handle
|
15
|
1136 |
"
|
0
|
1137 |
!
|
|
1138 |
|
106
|
1139 |
primLoadDynamicObject:pathName into:aHandle
|
|
1140 |
"load an object-file (map into my address space).
|
28
|
1141 |
Return an OS-handle (whatever that is) - where some space
|
106
|
1142 |
(a 3-element array) has to be passed in for this.
|
|
1143 |
The first two entries are used in a machine dependent way,
|
|
1144 |
and callers may not depend on what is found there
|
|
1145 |
(instead, only pass around handles transparently).
|
|
1146 |
This function is not supported on all architectures."
|
6
|
1147 |
|
|
1148 |
%{ /* UNLIMITEDSTACK */
|
|
1149 |
|
106
|
1150 |
if (! __isArray(aHandle)
|
|
1151 |
|| (_arraySize(aHandle) < 3)) {
|
|
1152 |
return nil;
|
|
1153 |
}
|
|
1154 |
|
15
|
1155 |
#ifdef GNU_DL
|
106
|
1156 |
{
|
20
|
1157 |
static firstCall = 1;
|
|
1158 |
extern char *__myName__;
|
|
1159 |
|
|
1160 |
if (firstCall) {
|
45
|
1161 |
firstCall = 0;
|
|
1162 |
(void) dld_init (__myName__);
|
20
|
1163 |
}
|
|
1164 |
|
15
|
1165 |
if (__isString(pathName)) {
|
98
|
1166 |
if (dld_link(__stringVal(pathName))) {
|
90
|
1167 |
if (ObjectFileLoader_Verbose == true) {
|
98
|
1168 |
printf ("link file %s failed\n", __stringVal(pathName));
|
90
|
1169 |
dld_perror("cant link");
|
|
1170 |
}
|
45
|
1171 |
ObjectFileLoader_LastError = @symbol(linkError);
|
|
1172 |
RETURN ( nil );
|
|
1173 |
}
|
106
|
1174 |
_ArrayInstPtr(aHandle)->a_element[0] = pathName;
|
|
1175 |
__STORE(aHandle, pathName);
|
|
1176 |
RETURN ( aHandle );
|
15
|
1177 |
}
|
|
1178 |
RETURN ( nil );
|
106
|
1179 |
}
|
15
|
1180 |
#endif
|
|
1181 |
|
95
|
1182 |
#ifdef DL1_6
|
106
|
1183 |
{
|
95
|
1184 |
extern char *__myName__;
|
|
1185 |
char *ldname;
|
106
|
1186 |
OBJ tmpName;
|
95
|
1187 |
|
|
1188 |
if (__isString(pathName)) {
|
98
|
1189 |
if ( dl_loadmod_only(__myName__, __stringVal(pathName), &ldname) == 0 ) {
|
95
|
1190 |
if (ObjectFileLoader_Verbose == true) {
|
98
|
1191 |
printf ("link file %s failed\n", __stringVal(pathName));
|
95
|
1192 |
}
|
|
1193 |
RETURN ( nil );
|
|
1194 |
}
|
|
1195 |
/*
|
|
1196 |
* returns the name of the temporary ld-file
|
|
1197 |
* use that as handle ...
|
|
1198 |
*/
|
106
|
1199 |
tmpName = _MKSTRING(ldname);
|
|
1200 |
_ArrayInstPtr(aHandle)->a_element[0] = tmpName;
|
|
1201 |
__STORE(aHandle, tmpName);
|
|
1202 |
RETURN ( aHandle );
|
95
|
1203 |
}
|
|
1204 |
RETURN ( nil );
|
106
|
1205 |
}
|
95
|
1206 |
#endif
|
|
1207 |
|
96
|
1208 |
#ifdef AIX_DL
|
106
|
1209 |
{
|
96
|
1210 |
extern char *__myName__;
|
|
1211 |
char *ldname;
|
|
1212 |
int *handle;
|
100
|
1213 |
extern errno;
|
96
|
1214 |
|
|
1215 |
if (__isString(pathName)) {
|
106
|
1216 |
if ( (handle = (int *) load(__stringVal(pathName), 0, 0)) == 0 ) {
|
|
1217 |
if (ObjectFileLoader_Verbose == true) {
|
|
1218 |
char *messages[64];
|
|
1219 |
int i;
|
100
|
1220 |
|
106
|
1221 |
printf ("load file %s failed errno=%d\n",
|
100
|
1222 |
__stringVal(pathName), errno);
|
106
|
1223 |
switch (errno) {
|
|
1224 |
case ENOEXEC:
|
|
1225 |
printf(" load messages:\n");
|
|
1226 |
loadquery(L_GETMESSAGES, messages, sizeof(messages));
|
|
1227 |
for (i=0; messages[i]; i++) {
|
|
1228 |
printf(" %s\n", messages[i]);
|
|
1229 |
}
|
|
1230 |
break;
|
97
|
1231 |
}
|
|
1232 |
}
|
106
|
1233 |
RETURN ( nil );
|
|
1234 |
}
|
|
1235 |
if (ObjectFileLoader_Verbose == true) {
|
|
1236 |
printf("load %s handle = %x\n", __stringVal(pathName), handle);
|
|
1237 |
}
|
96
|
1238 |
|
106
|
1239 |
_ArrayInstPtr(aHandle)->a_element[0] = _MKSMALLINT( (int)handle & 0xFFFF );
|
|
1240 |
_ArrayInstPtr(aHandle)->a_element[1] = _MKSMALLINT( ((int)handle >> 16) & 0xFFFF );
|
|
1241 |
__STORE(aHandle, pathName);
|
|
1242 |
RETURN (aHandle);
|
96
|
1243 |
}
|
|
1244 |
RETURN ( nil );
|
106
|
1245 |
}
|
96
|
1246 |
#endif
|
|
1247 |
|
15
|
1248 |
#ifdef SYSV4_DL
|
106
|
1249 |
{
|
15
|
1250 |
void *handle;
|
98
|
1251 |
char *nm;
|
15
|
1252 |
|
|
1253 |
if ((pathName == nil) || __isString(pathName)) {
|
106
|
1254 |
handle = dlopen(pathName == nil ? (char *)0 : __stringVal(pathName), RTLD_NOW);
|
15
|
1255 |
|
106
|
1256 |
if (! handle) {
|
|
1257 |
fprintf(stderr, "dlopen %s error: <%s>\n",
|
98
|
1258 |
__stringVal(pathName), dlerror());
|
106
|
1259 |
ObjectFileLoader_LastError = @symbol(linkError);
|
|
1260 |
RETURN (nil);
|
|
1261 |
}
|
28
|
1262 |
|
106
|
1263 |
if (ObjectFileLoader_Verbose == true) {
|
|
1264 |
printf("open %s handle = %x\n", __stringVal(pathName), handle);
|
45
|
1265 |
}
|
106
|
1266 |
|
|
1267 |
_ArrayInstPtr(aHandle)->a_element[0] = _MKSMALLINT( (int)handle & 0xFFFF );
|
|
1268 |
_ArrayInstPtr(aHandle)->a_element[1] = _MKSMALLINT( ((int)handle >> 16) & 0xFFFF );
|
|
1269 |
RETURN (aHandle);
|
15
|
1270 |
}
|
106
|
1271 |
}
|
15
|
1272 |
#endif
|
|
1273 |
|
|
1274 |
#ifdef SUN_DL
|
106
|
1275 |
{
|
6
|
1276 |
void *handle;
|
|
1277 |
|
15
|
1278 |
if ((pathName == nil) || __isString(pathName)) {
|
106
|
1279 |
if (pathName == nil)
|
|
1280 |
handle = dlopen((char *)0, 1);
|
|
1281 |
else
|
|
1282 |
handle = dlopen(__stringVal(pathName), 1);
|
15
|
1283 |
|
106
|
1284 |
if (! handle) {
|
|
1285 |
fprintf(stderr, "dlopen %s error: <%s>\n",
|
98
|
1286 |
__stringVal(pathName), dlerror());
|
106
|
1287 |
ObjectFileLoader_LastError = @symbol(linkError);
|
|
1288 |
RETURN (nil);
|
|
1289 |
}
|
28
|
1290 |
|
106
|
1291 |
if (ObjectFileLoader_Verbose == true) {
|
|
1292 |
printf("open %s handle = %x\n", __stringVal(pathName), handle);
|
45
|
1293 |
}
|
106
|
1294 |
|
|
1295 |
_ArrayInstPtr(aHandle)->a_element[0] = _MKSMALLINT( (int)handle & 0xFFFF );
|
|
1296 |
_ArrayInstPtr(aHandle)->a_element[1] = _MKSMALLINT( ((int)handle >> 16) & 0xFFFF );
|
|
1297 |
RETURN (aHandle);
|
6
|
1298 |
}
|
106
|
1299 |
}
|
6
|
1300 |
#endif
|
15
|
1301 |
|
|
1302 |
#ifdef NEXT_DL
|
106
|
1303 |
{
|
6
|
1304 |
long result;
|
|
1305 |
char *files[2];
|
|
1306 |
NXStream *errOut;
|
|
1307 |
|
15
|
1308 |
if (__isString(pathName)) {
|
98
|
1309 |
files[0] = (char *) __stringVal(pathName);
|
45
|
1310 |
files[1] = (char *) 0;
|
|
1311 |
errOut = NXOpenFile(2, 2);
|
|
1312 |
result = rld_load(errOut,
|
|
1313 |
(struct mach_header **)0,
|
|
1314 |
files,
|
|
1315 |
(char *)0);
|
|
1316 |
NXClose(errOut);
|
|
1317 |
if (! result) {
|
|
1318 |
ObjectFileLoader_LastError = @symbol(linkError);
|
98
|
1319 |
fprintf(stderr, "rld_load %s failed\n", __stringVal(pathName));
|
45
|
1320 |
RETURN (nil);
|
|
1321 |
}
|
15
|
1322 |
|
45
|
1323 |
if (ObjectFileLoader_Verbose == true)
|
98
|
1324 |
printf("rld_load %s ok\n", __stringVal(pathName));
|
28
|
1325 |
|
106
|
1326 |
_ArrayInstPtr(aHandle)->a_element[0] = pathName;
|
|
1327 |
__STORE(aHandle, pathName);
|
|
1328 |
RETURN ( aHandle );
|
6
|
1329 |
}
|
106
|
1330 |
}
|
6
|
1331 |
#endif
|
15
|
1332 |
%}.
|
28
|
1333 |
LastError := #notImplemented.
|
|
1334 |
^ nil
|
6
|
1335 |
!
|
|
1336 |
|
107
|
1337 |
unloadDynamicObject:handle
|
45
|
1338 |
"close an object-file (unmap from my address space)
|
106
|
1339 |
and remove the entry from the remembered object file set.
|
|
1340 |
This is a low-level entry, which does not care if there are
|
|
1341 |
still any code references (from blocks or methods) to this
|
|
1342 |
module. Calling it for still living classes will definitely
|
|
1343 |
lead to some fatal conditions to occur later."
|
45
|
1344 |
|
|
1345 |
|key|
|
|
1346 |
|
110
|
1347 |
'unload module name=' infoPrint. (handle at:3) infoPrintNL.
|
|
1348 |
|
107
|
1349 |
self performModuleDeInit:handle.
|
|
1350 |
|
|
1351 |
(self primUnloadDynamicObject:handle) ifTrue:[
|
45
|
1352 |
"
|
|
1353 |
remove from loaded objects
|
|
1354 |
"
|
|
1355 |
LoadedObjects notNil ifTrue:[
|
95
|
1356 |
key := LoadedObjects keyAtEqualValue:handle.
|
45
|
1357 |
key notNil ifTrue:[
|
|
1358 |
LoadedObjects removeKey:key
|
|
1359 |
]
|
92
|
1360 |
].
|
|
1361 |
Smalltalk flushCachedClasses
|
45
|
1362 |
] ifFalse:[
|
107
|
1363 |
self error:'unloadDynamic failed'
|
45
|
1364 |
]
|
|
1365 |
!
|
|
1366 |
|
107
|
1367 |
primUnloadDynamicObject:aHandle
|
|
1368 |
"unload an object-file (unmap from my address space).
|
106
|
1369 |
This is a low-level entry, which does not care if there are
|
|
1370 |
still any code references (from blocks or methods) to this
|
|
1371 |
module. Calling it for still living classes will definitely
|
|
1372 |
lead to some fatal conditions to occur later."
|
45
|
1373 |
|
15
|
1374 |
%{
|
106
|
1375 |
OBJ sysHandle1, sysHandle2;
|
|
1376 |
|
|
1377 |
if (! __isArray(aHandle)
|
|
1378 |
|| (_arraySize(aHandle) < 3)) {
|
|
1379 |
return false;
|
|
1380 |
}
|
|
1381 |
|
|
1382 |
sysHandle1 = __ArrayInstPtr(aHandle)->a_element[0];
|
|
1383 |
sysHandle2 = __ArrayInstPtr(aHandle)->a_element[1];
|
|
1384 |
|
15
|
1385 |
#ifdef GNU_DL
|
106
|
1386 |
if (__isString(sysHandle1)) {
|
|
1387 |
if (dld_unlink_by_file(__stringVal(sysHandle1), 1)) {
|
90
|
1388 |
if (ObjectFileLoader_Verbose == true) {
|
106
|
1389 |
printf ("unlink file %s failed\n", __stringVal(sysHandle1));
|
90
|
1390 |
dld_perror("cant unlink");
|
|
1391 |
}
|
45
|
1392 |
RETURN (false);
|
|
1393 |
}
|
|
1394 |
RETURN (true);
|
15
|
1395 |
}
|
45
|
1396 |
RETURN (false);
|
15
|
1397 |
#endif
|
95
|
1398 |
|
15
|
1399 |
#ifdef SYSV4_DL
|
106
|
1400 |
{
|
0
|
1401 |
void *h;
|
|
1402 |
int val;
|
106
|
1403 |
OBJ low = sysHandle1, hi = sysHandle2;
|
0
|
1404 |
|
60
|
1405 |
if (__bothSmallInteger(low, hi)) {
|
45
|
1406 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
1407 |
h = (void *)(val);
|
|
1408 |
if (ObjectFileLoader_Verbose == true)
|
|
1409 |
printf("close handle = %x\n", h);
|
|
1410 |
if (dlclose(h) != 0) {
|
|
1411 |
fprintf(stderr, "dlclose failed with:<%s>\n", dlerror());
|
|
1412 |
RETURN (false);
|
|
1413 |
}
|
|
1414 |
RETURN (true);
|
0
|
1415 |
}
|
106
|
1416 |
}
|
0
|
1417 |
#endif
|
15
|
1418 |
|
100
|
1419 |
#ifdef SUN_DL
|
106
|
1420 |
{
|
100
|
1421 |
void *h;
|
|
1422 |
int val;
|
106
|
1423 |
OBJ low = sysHandle1, hi = sysHandle2;
|
100
|
1424 |
|
|
1425 |
if (__bothSmallInteger(low, hi)) {
|
|
1426 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
1427 |
h = (void *)(val);
|
|
1428 |
if (ObjectFileLoader_Verbose == true)
|
|
1429 |
printf("close handle = %x\n", h);
|
|
1430 |
dlclose(h);
|
|
1431 |
RETURN (true);
|
|
1432 |
}
|
106
|
1433 |
}
|
100
|
1434 |
#endif
|
|
1435 |
|
96
|
1436 |
#ifdef AIX_DL
|
106
|
1437 |
{
|
97
|
1438 |
int *h;
|
96
|
1439 |
int val;
|
106
|
1440 |
OBJ low = sysHandle1, hi = sysHandle2;
|
96
|
1441 |
|
|
1442 |
if (__bothSmallInteger(low, hi)) {
|
97
|
1443 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
1444 |
h = (int *)(val);
|
|
1445 |
if (ObjectFileLoader_Verbose == true)
|
|
1446 |
printf("unload handle = %x\n", h);
|
|
1447 |
if ( unload(h) != 0) {
|
|
1448 |
fprintf(stderr, "unload failed\n");
|
|
1449 |
RETURN (false);
|
|
1450 |
}
|
|
1451 |
RETURN (true);
|
96
|
1452 |
}
|
106
|
1453 |
}
|
96
|
1454 |
#endif
|
45
|
1455 |
%}.
|
|
1456 |
^ false
|
0
|
1457 |
!
|
|
1458 |
|
20
|
1459 |
isSmalltalkObject:handle
|
|
1460 |
"return true, if the loaded object is a smalltalk object module"
|
|
1461 |
|
|
1462 |
"not yet implemented - stc_compiled_smalltalk is a static symbol,
|
|
1463 |
not found in list - need nm-interface, or nlist-walker
|
|
1464 |
"
|
|
1465 |
^ true.
|
|
1466 |
|
|
1467 |
(self getSymbol:'__stc_compiled_smalltalk' function:true from:handle) notNil ifTrue:[^ true].
|
|
1468 |
(self getSymbol:'__stc_compiled_smalltalk' function:false from:handle) notNil ifTrue:[^ true].
|
|
1469 |
^ false
|
|
1470 |
!
|
|
1471 |
|
|
1472 |
isCPlusPlusObject:handle
|
|
1473 |
"return true, if the loaded object is a c++ object module"
|
|
1474 |
|
|
1475 |
(self getSymbol:'__gnu_compiled_cplusplus' function:true from:handle) notNil ifTrue:[^ true].
|
|
1476 |
(self getSymbol:'__CTOR_LIST__' function:true from:handle) notNil ifTrue:[^ true].
|
|
1477 |
(self getSymbol:'__CTOR_LIST__' function:false from:handle) notNil ifTrue:[^ true].
|
|
1478 |
(self getSymbol:'__gnu_compiled_cplusplus' function:false from:handle) notNil ifTrue:[^ true].
|
|
1479 |
^ false
|
|
1480 |
!
|
|
1481 |
|
106
|
1482 |
isObjectiveCObject:handle
|
|
1483 |
"not yet implemented"
|
|
1484 |
|
|
1485 |
^ false
|
|
1486 |
!
|
|
1487 |
|
28
|
1488 |
namesMatching:aPattern segment:segmentPattern in:aFileName
|
100
|
1489 |
"this is rubbish - it will vanish soon"
|
|
1490 |
|
20
|
1491 |
|p l s addr segment name entry|
|
|
1492 |
|
100
|
1493 |
OperatingSystem getSystemType = 'aix' ifTrue:[
|
|
1494 |
^ nil
|
|
1495 |
].
|
|
1496 |
|
20
|
1497 |
l := OrderedCollection new.
|
28
|
1498 |
p := PipeStream readingFrom:(self nm:aFileName).
|
20
|
1499 |
p isNil ifTrue:[
|
45
|
1500 |
('cannot read names from ' , aFileName) errorPrintNL.
|
|
1501 |
^ nil
|
20
|
1502 |
].
|
|
1503 |
[p atEnd] whileFalse:[
|
45
|
1504 |
entry := p nextLine.
|
|
1505 |
Verbose ifTrue:[
|
107
|
1506 |
entry infoPrintNL.
|
45
|
1507 |
].
|
|
1508 |
entry notNil ifTrue:[
|
|
1509 |
s := ReadStream on:entry.
|
60
|
1510 |
addr := s nextAlphaNumericWord.
|
|
1511 |
segment := s nextAlphaNumericWord.
|
45
|
1512 |
name := s upToEnd withoutSeparators.
|
|
1513 |
(addr notNil and:[segment notNil and:[name notNil]]) ifTrue:[
|
|
1514 |
(aPattern match:name) ifTrue:[
|
|
1515 |
(segmentPattern isNil or:[segmentPattern match:segment]) ifTrue:[
|
|
1516 |
l add:name.
|
|
1517 |
Verbose ifTrue:[
|
107
|
1518 |
('found name: ' , name) infoPrintNL.
|
45
|
1519 |
]
|
|
1520 |
] ifFalse:[
|
|
1521 |
Verbose ifTrue:[
|
107
|
1522 |
name infoPrint. ' segment mismatch ' infoPrint.
|
|
1523 |
segmentPattern infoPrint. ' ' infoPrint. segment infoPrintNL.
|
45
|
1524 |
]
|
|
1525 |
]
|
|
1526 |
]
|
|
1527 |
]
|
|
1528 |
]
|
20
|
1529 |
].
|
|
1530 |
p close.
|
|
1531 |
^ l
|
|
1532 |
!
|
|
1533 |
|
|
1534 |
getFunction:aString from:handle
|
|
1535 |
"return the address of a function from a dynamically loaded object file.
|
106
|
1536 |
Handle must be the one returned previously from loadDynamicObject.
|
20
|
1537 |
Return the address of the function, or nil on any error."
|
|
1538 |
|
100
|
1539 |
|fName|
|
|
1540 |
|
|
1541 |
OperatingSystem getSystemType = 'aix' ifTrue:[
|
|
1542 |
fName := '.' , aString
|
|
1543 |
] ifFalse:[
|
|
1544 |
fName := aString
|
|
1545 |
].
|
|
1546 |
^ self getSymbol:fName function:true from:handle
|
20
|
1547 |
!
|
|
1548 |
|
106
|
1549 |
getSymbol:aString function:isFunction from:aHandle
|
20
|
1550 |
"return the address of a symbol/function from a dynamically loaded object file.
|
106
|
1551 |
Handle must be the one returned previously from loadDynamicObject.
|
0
|
1552 |
Return the address of the symbol, or nil on any error."
|
|
1553 |
|
95
|
1554 |
|lowAddr hiAddr|
|
0
|
1555 |
|
20
|
1556 |
%{ /* STACK: 20000 */
|
|
1557 |
|
106
|
1558 |
OBJ sysHandle1, sysHandle2;
|
|
1559 |
|
|
1560 |
if (! __isArray(aHandle)
|
|
1561 |
|| (_arraySize(aHandle) < 3)) {
|
|
1562 |
return false;
|
|
1563 |
}
|
|
1564 |
|
|
1565 |
sysHandle1 = __ArrayInstPtr(aHandle)->a_element[0];
|
|
1566 |
sysHandle2 = __ArrayInstPtr(aHandle)->a_element[1];
|
|
1567 |
|
15
|
1568 |
#ifdef GNU_DL
|
108
|
1569 |
{
|
15
|
1570 |
void (*func)();
|
20
|
1571 |
unsigned long addr;
|
|
1572 |
char *name;
|
15
|
1573 |
|
|
1574 |
if (__isString(aString)) {
|
98
|
1575 |
name = (char *) __stringVal(aString);
|
45
|
1576 |
if (isFunction == false) {
|
|
1577 |
addr = dld_get_symbol(name);
|
|
1578 |
} else {
|
|
1579 |
func = (void (*) ()) dld_get_func(name);
|
|
1580 |
if (func) {
|
|
1581 |
if (ObjectFileLoader_Verbose == true)
|
|
1582 |
printf("addr of %s = %x\n", name, (INT)func);
|
|
1583 |
if (dld_function_executable_p(name)) {
|
|
1584 |
lowAddr = _MKSMALLINT( (INT)func & 0xFFFF );
|
|
1585 |
hiAddr = _MKSMALLINT( ((INT)func >> 16) & 0xFFFF );
|
|
1586 |
} else {
|
|
1587 |
char **undefNames;
|
|
1588 |
char **nm;
|
|
1589 |
int i;
|
20
|
1590 |
|
45
|
1591 |
if (ObjectFileLoader_Verbose == true) {
|
|
1592 |
printf ("function %s not executable\n", name);
|
|
1593 |
dld_perror("not executable");
|
20
|
1594 |
|
45
|
1595 |
printf("undefined:\n");
|
|
1596 |
nm = undefNames = dld_list_undefined_sym();
|
|
1597 |
for (i=dld_undefined_sym_count; i; i--) {
|
|
1598 |
printf(" %s\n", *nm++);
|
|
1599 |
}
|
|
1600 |
}
|
|
1601 |
free(undefNames);
|
|
1602 |
}
|
|
1603 |
} else {
|
90
|
1604 |
if (ObjectFileLoader_Verbose == true) {
|
|
1605 |
printf ("function %s not found\n", name);
|
|
1606 |
dld_perror("get_func");
|
|
1607 |
}
|
45
|
1608 |
}
|
|
1609 |
}
|
15
|
1610 |
}
|
108
|
1611 |
}
|
95
|
1612 |
#endif /* GNU_DL */
|
15
|
1613 |
|
95
|
1614 |
#ifdef DL1_6
|
108
|
1615 |
{
|
0
|
1616 |
void *h;
|
|
1617 |
void *addr;
|
|
1618 |
int val;
|
|
1619 |
|
95
|
1620 |
if (__isString(aString)) {
|
106
|
1621 |
if (__isString(sysHandle1)) {
|
95
|
1622 |
if (ObjectFileLoader_Verbose == true)
|
|
1623 |
printf("get sym <%s> handle = %x\n",
|
106
|
1624 |
__stringVal(aString), __stringVal(sysHandle1));
|
|
1625 |
addr = dl_getsymbol(__stringVal(sysHandle1), __stringVal(aString));
|
95
|
1626 |
if (addr) {
|
|
1627 |
if (ObjectFileLoader_Verbose == true)
|
|
1628 |
printf("addr = %x\n", addr);
|
|
1629 |
lowAddr = _MKSMALLINT( (int)addr & 0xFFFF );
|
|
1630 |
hiAddr = _MKSMALLINT( ((int)addr >> 16) & 0xFFFF );
|
|
1631 |
} else {
|
|
1632 |
if (ObjectFileLoader_Verbose == true)
|
98
|
1633 |
printf("dl_getsymbol %s failed\n", __stringVal(aString));
|
95
|
1634 |
}
|
|
1635 |
}
|
|
1636 |
}
|
108
|
1637 |
}
|
95
|
1638 |
#endif
|
|
1639 |
|
|
1640 |
#ifdef SYSV4_DL
|
106
|
1641 |
{
|
95
|
1642 |
void *h;
|
|
1643 |
void *addr;
|
|
1644 |
int val;
|
106
|
1645 |
OBJ low = sysHandle1, hi = sysHandle2;
|
95
|
1646 |
|
60
|
1647 |
if (__bothSmallInteger(low, hi)) {
|
45
|
1648 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
1649 |
h = (void *)(val);
|
|
1650 |
if (__isString(aString)) {
|
|
1651 |
if (ObjectFileLoader_Verbose == true)
|
98
|
1652 |
printf("get sym <%s> handle = %x\n", __stringVal(aString), h);
|
|
1653 |
addr = dlsym(h, (char *) __stringVal(aString));
|
45
|
1654 |
if (addr) {
|
107
|
1655 |
if (ObjectFileLoader_Verbose == true) {
|
|
1656 |
printf("dlsym %s ok; addr = %x\n", addr);
|
|
1657 |
}
|
45
|
1658 |
lowAddr = _MKSMALLINT( (int)addr & 0xFFFF );
|
|
1659 |
hiAddr = _MKSMALLINT( ((int)addr >> 16) & 0xFFFF );
|
|
1660 |
} else {
|
107
|
1661 |
if (ObjectFileLoader_Verbose == true) {
|
|
1662 |
printf("dlsym %s error: %s", __stringVal(aString), dlerror());
|
|
1663 |
}
|
45
|
1664 |
}
|
|
1665 |
}
|
0
|
1666 |
}
|
106
|
1667 |
}
|
0
|
1668 |
#endif
|
15
|
1669 |
|
96
|
1670 |
#ifdef AIX_DL
|
106
|
1671 |
{
|
108
|
1672 |
OBJ fileName;
|
96
|
1673 |
void *h;
|
|
1674 |
void *addr;
|
|
1675 |
int val;
|
97
|
1676 |
struct nlist nl[2];
|
106
|
1677 |
OBJ low = sysHandle1, hi = sysHandle2;
|
96
|
1678 |
|
|
1679 |
if (__bothSmallInteger(low, hi)
|
|
1680 |
&& __isString(fileName)) {
|
97
|
1681 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
1682 |
h = (void *)(val);
|
|
1683 |
if (__isString(aString)) {
|
|
1684 |
if (ObjectFileLoader_Verbose == true)
|
|
1685 |
printf("get sym <%s> handle = %x file = %s\n",
|
98
|
1686 |
__stringVal(aString), h, __stringVal(fileName));
|
96
|
1687 |
|
98
|
1688 |
nl[0].n_name = __stringVal(aString);
|
97
|
1689 |
nl[1].n_name = "";
|
96
|
1690 |
|
98
|
1691 |
if (nlist(__stringVal(fileName), &nl) == -1) {
|
97
|
1692 |
if (ObjectFileLoader_Verbose == true)
|
|
1693 |
printf("nlist error\n");
|
96
|
1694 |
} else {
|
100
|
1695 |
addr = (void *)((unsigned)nl[0].n_value + (unsigned)h);
|
|
1696 |
|
|
1697 |
if (ObjectFileLoader_Verbose == true) {
|
|
1698 |
printf("value=%x section=%d type=%x sclass=%d\n",
|
|
1699 |
nl[0].n_value, nl[0].n_scnum, nl[0].n_type, nl[0].n_sclass);
|
|
1700 |
printf("addr = %x\n", addr);
|
|
1701 |
}
|
|
1702 |
|
|
1703 |
lowAddr = _MKSMALLINT( (int)addr & 0xFFFF );
|
|
1704 |
hiAddr = _MKSMALLINT( ((int)addr >> 16) & 0xFFFF );
|
96
|
1705 |
}
|
|
1706 |
}
|
|
1707 |
}
|
106
|
1708 |
}
|
96
|
1709 |
#endif
|
|
1710 |
|
15
|
1711 |
#ifdef SUN_DL
|
106
|
1712 |
{
|
3
|
1713 |
void *h;
|
|
1714 |
void *addr;
|
|
1715 |
int val;
|
106
|
1716 |
OBJ low = sysHandle1, hi = sysHandle2;
|
3
|
1717 |
|
60
|
1718 |
if (__bothSmallInteger(low, hi)) {
|
45
|
1719 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
1720 |
h = (void *)(val);
|
|
1721 |
if (__isString(aString)) {
|
|
1722 |
if (ObjectFileLoader_Verbose == true)
|
98
|
1723 |
printf("get sym <%s> handle = %x\n", __stringVal(aString), h);
|
|
1724 |
addr = dlsym(h, __stringVal(aString));
|
45
|
1725 |
if (addr) {
|
|
1726 |
if (ObjectFileLoader_Verbose == true)
|
|
1727 |
printf("addr = %x\n", addr);
|
|
1728 |
lowAddr = _MKSMALLINT( (int)addr & 0xFFFF );
|
|
1729 |
hiAddr = _MKSMALLINT( ((int)addr >> 16) & 0xFFFF );
|
|
1730 |
} else {
|
|
1731 |
if (ObjectFileLoader_Verbose == true)
|
98
|
1732 |
printf("dlsym %s error: %s\n", __stringVal(aString), dlerror());
|
45
|
1733 |
}
|
|
1734 |
}
|
3
|
1735 |
}
|
106
|
1736 |
}
|
3
|
1737 |
#endif
|
15
|
1738 |
|
|
1739 |
#ifdef NEXT_DL
|
106
|
1740 |
{
|
0
|
1741 |
unsigned long addr;
|
|
1742 |
long result;
|
|
1743 |
NXStream *errOut;
|
|
1744 |
|
15
|
1745 |
if (__isString(aString)) {
|
45
|
1746 |
if (ObjectFileLoader_Verbose == true)
|
98
|
1747 |
printf("get sym <%s>\n", __stringVal(aString));
|
45
|
1748 |
errOut = NXOpenFile(2, 2);
|
|
1749 |
result = rld_lookup(errOut,
|
98
|
1750 |
(char *) __stringVal(aString),
|
45
|
1751 |
&addr);
|
|
1752 |
NXClose(errOut);
|
|
1753 |
if (result) {
|
|
1754 |
if (ObjectFileLoader_Verbose == true)
|
|
1755 |
printf("addr = %x\n", addr);
|
|
1756 |
lowAddr = _MKSMALLINT( (int)addr & 0xFFFF );
|
|
1757 |
hiAddr = _MKSMALLINT( ((int)addr >> 16) & 0xFFFF );
|
|
1758 |
}
|
0
|
1759 |
}
|
106
|
1760 |
}
|
96
|
1761 |
#endif
|
95
|
1762 |
%}.
|
|
1763 |
|
0
|
1764 |
lowAddr notNil ifTrue:[
|
45
|
1765 |
^ (hiAddr * 16r10000) + lowAddr
|
0
|
1766 |
].
|
|
1767 |
^ nil
|
|
1768 |
!
|
|
1769 |
|
106
|
1770 |
getListOfUndefinedSymbolsFrom:aHandle
|
20
|
1771 |
"return a collection of undefined symbols in a dynamically loaded object file.
|
106
|
1772 |
Handle must be the one returned previously from loadDynamicObject."
|
20
|
1773 |
|
|
1774 |
|list|
|
|
1775 |
|
|
1776 |
list := Array new:100. "no more than 100 symbols"
|
|
1777 |
%{
|
|
1778 |
|
|
1779 |
#ifdef GNU_DL
|
|
1780 |
void (*func)();
|
|
1781 |
unsigned long addr;
|
|
1782 |
char *name;
|
|
1783 |
int nMax;
|
|
1784 |
|
|
1785 |
if (__isArray(list)) {
|
45
|
1786 |
char **undefNames;
|
|
1787 |
char **nm;
|
|
1788 |
int index;
|
20
|
1789 |
|
45
|
1790 |
nMax = _arraySize(list);
|
20
|
1791 |
|
45
|
1792 |
nm = undefNames = dld_list_undefined_sym();
|
|
1793 |
for (index = 0; index < dld_undefined_sym_count; index++) {
|
|
1794 |
_ArrayInstPtr(list)->a_element[index] = _MKSTRING(*nm++);
|
|
1795 |
if (index == nMax)
|
|
1796 |
break;
|
|
1797 |
}
|
|
1798 |
free(undefNames);
|
20
|
1799 |
}
|
|
1800 |
#endif
|
|
1801 |
|
95
|
1802 |
#ifdef DL1_6
|
|
1803 |
/*
|
|
1804 |
* dont know how to do it
|
|
1805 |
*/
|
|
1806 |
#endif
|
|
1807 |
|
20
|
1808 |
#ifdef SYSV4_DL
|
|
1809 |
/*
|
|
1810 |
* dont know how to do it
|
|
1811 |
*/
|
|
1812 |
#endif
|
|
1813 |
|
|
1814 |
#ifdef SUN_DL
|
|
1815 |
/*
|
|
1816 |
* dont know how to do it
|
|
1817 |
*/
|
|
1818 |
#endif
|
|
1819 |
|
|
1820 |
#ifdef NEXT_DL
|
|
1821 |
/*
|
|
1822 |
* dont know how to do it
|
|
1823 |
*/
|
|
1824 |
#endif
|
|
1825 |
%}.
|
|
1826 |
^ list
|
|
1827 |
!
|
|
1828 |
|
0
|
1829 |
releaseSymbolTable
|
|
1830 |
"this is needed on NeXT to forget loaded names. If this wasnt done,
|
15
|
1831 |
the same class could not be loaded in again due to multiple defines.
|
0
|
1832 |
On other architectures, this is not needed and therefore a noop."
|
|
1833 |
|
|
1834 |
%{
|
15
|
1835 |
#ifdef NEXT_DL
|
0
|
1836 |
NXStream *errOut;
|
|
1837 |
|
|
1838 |
errOut = NXOpenFile(2, 2);
|
|
1839 |
rld_unload_all(errOut, (long)0);
|
|
1840 |
rld_load_basefile(errOut, "smalltalk");
|
|
1841 |
NXClose(errOut);
|
|
1842 |
#endif
|
|
1843 |
%}
|
|
1844 |
!
|
|
1845 |
|
113
|
1846 |
allModulesDo:aBlock
|
|
1847 |
%{
|
|
1848 |
__REGISTRATION_DO_BLOCK(&aBlock COMMA_SND);
|
|
1849 |
%}
|
|
1850 |
!
|
|
1851 |
|
107
|
1852 |
performModuleDeInit:handle
|
|
1853 |
|id|
|
|
1854 |
|
110
|
1855 |
'deInit module name=' infoPrint. (handle at:3) infoPrint.
|
|
1856 |
' id=' infoPrint. (handle at:4) infoPrintNL.
|
|
1857 |
|
107
|
1858 |
id := handle at:4.
|
|
1859 |
%{
|
|
1860 |
__UNREGISTER_BY_ID(__intVal(id));
|
|
1861 |
%}
|
|
1862 |
!
|
|
1863 |
|
106
|
1864 |
performModuleInitAt:initAddr identifyAs:handle
|
20
|
1865 |
"
|
106
|
1866 |
Initialize a smalltalk module.
|
82
|
1867 |
need 4 passes to init: 0: let module register itself & create its pools/globals
|
97
|
1868 |
0b check if modules superclasses are all loaded
|
82
|
1869 |
1: let it get var-refs to other pools/globals
|
|
1870 |
2: let it install install class, methods and literals
|
106
|
1871 |
3: let it send #initialize to its class object
|
20
|
1872 |
"
|
53
|
1873 |
|
83
|
1874 |
|status badClassName|
|
|
1875 |
|
97
|
1876 |
"/
|
|
1877 |
"/ let it register itself
|
|
1878 |
"/ and define its globals
|
|
1879 |
"/
|
81
|
1880 |
Verbose ifTrue:[
|
107
|
1881 |
'phase 0 ...' infoPrintNL
|
81
|
1882 |
].
|
82
|
1883 |
self callInitFunctionAt:initAddr
|
|
1884 |
specialInit:true
|
|
1885 |
forceOld:true
|
|
1886 |
interruptable:false
|
106
|
1887 |
argument:0
|
|
1888 |
identifyAs:handle.
|
82
|
1889 |
|
97
|
1890 |
"/
|
|
1891 |
"/ check if superclasses are present
|
|
1892 |
"/
|
83
|
1893 |
(status := self loadStatus) == 0 ifFalse:[
|
|
1894 |
badClassName := self classNameThatFailed.
|
|
1895 |
status == -1 ifTrue:[
|
|
1896 |
Transcript showCr:'load failed - missing class: ' , badClassName.
|
95
|
1897 |
^ Array with:#missingClass:
|
|
1898 |
with:badClassName
|
83
|
1899 |
].
|
95
|
1900 |
status == -2 ifTrue:[
|
|
1901 |
Transcript showCr:'load failed - version mismatch: ' , badClassName.
|
|
1902 |
^ Array with:#versionMismatch:
|
|
1903 |
with:badClassName
|
|
1904 |
].
|
|
1905 |
Transcript showCr:'load failed'.
|
|
1906 |
^ Array with:#loadFailed
|
83
|
1907 |
].
|
|
1908 |
|
97
|
1909 |
"/
|
|
1910 |
"/ remaining initialization
|
|
1911 |
"/
|
81
|
1912 |
Verbose ifTrue:[
|
107
|
1913 |
'phase 1 ...' infoPrintNL
|
81
|
1914 |
].
|
82
|
1915 |
self moduleInit:1 forceOld:true interruptable:false.
|
|
1916 |
|
81
|
1917 |
Verbose ifTrue:[
|
107
|
1918 |
'phase 2 ...' infoPrintNL
|
81
|
1919 |
].
|
82
|
1920 |
self moduleInit:2 forceOld:true interruptable:false.
|
|
1921 |
|
81
|
1922 |
Verbose ifTrue:[
|
107
|
1923 |
'phase 3 ...' infoPrintNL
|
81
|
1924 |
].
|
82
|
1925 |
self moduleInit:3 forceOld:false interruptable:true.
|
95
|
1926 |
^ Array with:#ok
|
20
|
1927 |
!
|
|
1928 |
|
83
|
1929 |
loadStatus
|
|
1930 |
"ask VM if class-hierarchy has been completely loaded"
|
|
1931 |
|
84
|
1932 |
|checker checkSymbol|
|
|
1933 |
|
|
1934 |
checker := self.
|
|
1935 |
checkSymbol := #'superClassCheck:'.
|
|
1936 |
|
|
1937 |
%{ /* NOREGISTER */
|
|
1938 |
RETURN (_MKSMALLINT(__check_registration__(&checker, &checkSymbol)));
|
83
|
1939 |
%}
|
|
1940 |
!
|
|
1941 |
|
84
|
1942 |
superClassCheck:aClass
|
|
1943 |
"callBack from class registration code in VM: make certain, that aClass is
|
|
1944 |
loaded too ..."
|
|
1945 |
|
95
|
1946 |
Verbose ifTrue:[
|
|
1947 |
'checkCall for:' infoPrint. aClass name infoPrint. ' -> ' infoPrint.
|
|
1948 |
].
|
|
1949 |
aClass isBehavior ifFalse:[
|
|
1950 |
Verbose ifTrue:[
|
|
1951 |
'false' infoPrintNL.
|
|
1952 |
].
|
|
1953 |
'LOADER: check failed - no behavior' infoPrintNL.
|
|
1954 |
^ false
|
|
1955 |
].
|
96
|
1956 |
('LOADER: check for ' , aClass name , ' being loaded') infoPrintNL.
|
84
|
1957 |
aClass autoload.
|
110
|
1958 |
(aClass isBehavior and:[aClass isLoaded]) ifTrue:[
|
111
|
1959 |
('LOADER: ok, loaded. continue registration of actual class') infoPrintNL.
|
|
1960 |
^ true
|
95
|
1961 |
].
|
110
|
1962 |
('LOADER: nope not loaded. fail registration of actual class') infoPrintNL.
|
|
1963 |
^ false
|
84
|
1964 |
!
|
|
1965 |
|
83
|
1966 |
classNameThatFailed
|
|
1967 |
"ask VM for the name of the class that caused trouble"
|
|
1968 |
|
|
1969 |
%{
|
|
1970 |
extern char *__name_registration_failure__();
|
|
1971 |
|
|
1972 |
RETURN (_MKSTRING(__name_registration_failure__()));
|
|
1973 |
%}
|
|
1974 |
!
|
|
1975 |
|
106
|
1976 |
callInitFunctionAt:address specialInit:special forceOld:forceOld interruptable:interruptable argument:argument identifyAs:handle
|
|
1977 |
"call a function at address - a very dangerous operation.
|
0
|
1978 |
This is needed to call the classes init-function after loading in a
|
82
|
1979 |
class-object file or to call the CTORS when c++ modules are loaded.
|
|
1980 |
ForceOld (if true) will have the memory manager
|
37
|
1981 |
allocate things in oldSpace instead of newSpace.
|
|
1982 |
DANGER: Internal. Dont use in your programs."
|
0
|
1983 |
|
106
|
1984 |
|low hi lowAddr hiAddr moduleID|
|
0
|
1985 |
|
|
1986 |
hi := address // 16r10000.
|
|
1987 |
low := address \\ 16r10000.
|
106
|
1988 |
handle notNil ifTrue:[
|
107
|
1989 |
moduleID := handle at:4
|
106
|
1990 |
].
|
0
|
1991 |
%{
|
|
1992 |
void (*addr)();
|
|
1993 |
unsigned val;
|
|
1994 |
typedef void (*VOIDFUNC)();
|
19
|
1995 |
int savInt;
|
82
|
1996 |
int prevSpace, force;
|
20
|
1997 |
int arg = 0;
|
82
|
1998 |
int wasBlocked = 1;
|
95
|
1999 |
extern OBJ __BLOCKINTERRUPTS();
|
100
|
2000 |
extern int __oldSpaceSize(), __oldSpaceUsed();
|
0
|
2001 |
|
60
|
2002 |
if (__bothSmallInteger(low, hi)) {
|
82
|
2003 |
if (_isSmallInteger(argument)) {
|
|
2004 |
arg = __intVal(argument);
|
|
2005 |
|
|
2006 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
2007 |
addr = (VOIDFUNC) val;
|
|
2008 |
|
|
2009 |
/*
|
|
2010 |
* allow function to be interrupted
|
|
2011 |
*/
|
|
2012 |
if (interruptable != true) {
|
95
|
2013 |
wasBlocked = (__BLOCKINTERRUPTS() == true);
|
82
|
2014 |
}
|
|
2015 |
|
|
2016 |
force = (forceOld == true);
|
|
2017 |
if (force) {
|
100
|
2018 |
if ((__oldSpaceSize() - __oldSpaceUsed()) < (64*1024)) {
|
|
2019 |
__moreOldSpace(__thisContext, 64*1024);
|
|
2020 |
}
|
82
|
2021 |
prevSpace = __allocForceSpace(OLDSPACE);
|
|
2022 |
}
|
|
2023 |
|
106
|
2024 |
if (special == true) {
|
|
2025 |
if (__isSmallInteger(moduleID)) {
|
|
2026 |
__SET_MODULE_ID(__intVal(moduleID));
|
|
2027 |
}
|
82
|
2028 |
(*addr)(arg, __pRT__ COMMA_CON);
|
106
|
2029 |
__SET_MODULE_ID(0);
|
|
2030 |
} else {
|
82
|
2031 |
(*addr)(arg COMMA_CON);
|
106
|
2032 |
}
|
19
|
2033 |
|
82
|
2034 |
if (force) {
|
|
2035 |
__allocForceSpace(prevSpace);
|
|
2036 |
}
|
|
2037 |
|
|
2038 |
if (! wasBlocked) {
|
95
|
2039 |
__UNBLOCKINTERRUPTS();
|
82
|
2040 |
}
|
|
2041 |
RETURN (self);
|
45
|
2042 |
}
|
82
|
2043 |
}
|
|
2044 |
%}.
|
|
2045 |
self primitiveFailed
|
|
2046 |
!
|
|
2047 |
|
|
2048 |
moduleInit:phase forceOld:forceOld interruptable:interruptable
|
|
2049 |
"initialization phases after registration.
|
|
2050 |
DANGER: Pure magic; internal only -> dont use in your programs."
|
|
2051 |
|
|
2052 |
%{
|
|
2053 |
int savInt;
|
|
2054 |
int prevSpace, force;
|
|
2055 |
int arg = 0;
|
|
2056 |
int wasBlocked = 1;
|
95
|
2057 |
extern OBJ __BLOCKINTERRUPTS();
|
82
|
2058 |
|
|
2059 |
if (_isSmallInteger(phase)) {
|
|
2060 |
if (interruptable != true) {
|
95
|
2061 |
wasBlocked = (__BLOCKINTERRUPTS() == true);
|
82
|
2062 |
}
|
19
|
2063 |
|
82
|
2064 |
force = (forceOld == true);
|
|
2065 |
if (force) {
|
45
|
2066 |
prevSpace = __allocForceSpace(OLDSPACE);
|
|
2067 |
}
|
19
|
2068 |
|
82
|
2069 |
init_registered_modules(__intVal(phase) COMMA_CON);
|
|
2070 |
|
|
2071 |
if (force) {
|
|
2072 |
__allocForceSpace(prevSpace);
|
|
2073 |
}
|
|
2074 |
|
|
2075 |
if (! wasBlocked) {
|
95
|
2076 |
__UNBLOCKINTERRUPTS();
|
82
|
2077 |
}
|
|
2078 |
RETURN (self);
|
0
|
2079 |
}
|
82
|
2080 |
%}.
|
|
2081 |
self primitiveFailed
|
0
|
2082 |
! !
|
|
2083 |
|
20
|
2084 |
!ObjectFileLoader class methodsFor:'primitive loading'!
|
|
2085 |
|
|
2086 |
textSizeOf:aFileName
|
|
2087 |
"
|
|
2088 |
get the size of the text-segment (nBytes)
|
|
2089 |
"
|
|
2090 |
|
|
2091 |
%{ /* NOCONTEXT */
|
|
2092 |
#ifdef HAS_DL
|
|
2093 |
/*
|
|
2094 |
* not needed, if dynamic link facilities exist
|
|
2095 |
*/
|
|
2096 |
#else /* no DL-support */
|
|
2097 |
char *fname;
|
|
2098 |
int fd;
|
|
2099 |
|
|
2100 |
if (! __isString(aFileName)) {
|
45
|
2101 |
RETURN (nil);
|
20
|
2102 |
}
|
|
2103 |
|
98
|
2104 |
fname = (char *) __stringVal(aFileName);
|
20
|
2105 |
|
|
2106 |
# if defined(A_DOT_OUT) && !defined(ELF)
|
|
2107 |
# if !defined(sco) && !defined(isc)
|
|
2108 |
{
|
45
|
2109 |
struct exec header;
|
20
|
2110 |
|
45
|
2111 |
if ((fd = open(fname, 0)) < 0) {
|
|
2112 |
fprintf(stderr, "cannot open <%s>\n", fname);
|
|
2113 |
RETURN ( nil );
|
|
2114 |
}
|
|
2115 |
if (read(fd, &header, sizeof(header)) != sizeof(header)) {
|
|
2116 |
fprintf(stderr, "cannot read header of <%s>\n", fname);
|
|
2117 |
close(fd);
|
|
2118 |
RETURN ( nil );
|
|
2119 |
}
|
|
2120 |
close(fd);
|
20
|
2121 |
|
45
|
2122 |
if (N_MAGIC(header) != OMAGIC) {
|
|
2123 |
fprintf(stderr, "header is (0%o) %x - should be (0%o)%x\n",
|
|
2124 |
N_MAGIC(header), N_MAGIC(header),
|
|
2125 |
OMAGIC, OMAGIC);
|
|
2126 |
RETURN ( nil );
|
|
2127 |
}
|
|
2128 |
RETURN ( _MKSMALLINT(header.a_text) );
|
20
|
2129 |
}
|
|
2130 |
# endif
|
|
2131 |
# endif
|
|
2132 |
/*
|
|
2133 |
* need support for other headers ... (i.e. coff, elf)
|
|
2134 |
*/
|
|
2135 |
#endif
|
|
2136 |
%}.
|
|
2137 |
^ self error:'objectFile format not supported'
|
|
2138 |
!
|
|
2139 |
|
|
2140 |
dataSizeOf:aFileName
|
|
2141 |
"
|
|
2142 |
get the size of the data-segment (nBytes)
|
|
2143 |
"
|
|
2144 |
|
|
2145 |
%{ /* NOCONTEXT */
|
|
2146 |
#ifdef HAS_DL
|
|
2147 |
/*
|
|
2148 |
* not needed, if dynamic link facilities exist
|
|
2149 |
*/
|
|
2150 |
#else /* no DL-support */
|
|
2151 |
char *fname;
|
|
2152 |
int fd;
|
|
2153 |
|
|
2154 |
if (! __isString(aFileName)) {
|
45
|
2155 |
RETURN ( nil );
|
20
|
2156 |
}
|
|
2157 |
|
98
|
2158 |
fname = (char *) __stringVal(aFileName);
|
20
|
2159 |
|
|
2160 |
# if defined(A_DOT_OUT) && !defined(ELF)
|
|
2161 |
# if !defined(sco) && !defined(isc)
|
|
2162 |
{
|
45
|
2163 |
struct exec header;
|
|
2164 |
unsigned size;
|
20
|
2165 |
|
45
|
2166 |
if ((fd = open(fname, 0)) < 0) {
|
|
2167 |
fprintf(stderr, "cannot open <%s>\n", fname);
|
|
2168 |
RETURN ( nil );
|
|
2169 |
}
|
|
2170 |
if (read(fd, &header, sizeof(header)) != sizeof(header)) {
|
|
2171 |
fprintf(stderr, "cannot read header of <%s>\n", fname);
|
|
2172 |
close(fd);
|
|
2173 |
RETURN ( nil );
|
|
2174 |
}
|
|
2175 |
close(fd);
|
20
|
2176 |
|
45
|
2177 |
if (N_MAGIC(header) != OMAGIC) {
|
|
2178 |
fprintf(stderr, "header is (0%o) %x - should be (0%o)%x\n",
|
|
2179 |
N_MAGIC(header), N_MAGIC(header),
|
|
2180 |
OMAGIC, OMAGIC);
|
|
2181 |
RETURN ( nil );
|
|
2182 |
}
|
|
2183 |
size = header.a_data;
|
20
|
2184 |
# if defined(sinix) && defined(BSD)
|
45
|
2185 |
size += header.a_bss;
|
20
|
2186 |
# endif
|
45
|
2187 |
RETURN ( _MKSMALLINT(size) );
|
20
|
2188 |
}
|
|
2189 |
# endif
|
|
2190 |
# endif
|
|
2191 |
/*
|
|
2192 |
* need support for other headers ... (i.e. coff, elf)
|
|
2193 |
*/
|
|
2194 |
#endif
|
|
2195 |
%}
|
|
2196 |
.
|
|
2197 |
^ self error:'objectFile format not supported'
|
|
2198 |
!
|
|
2199 |
|
|
2200 |
loadObjectFile:aFileName textAddr:textAddr textSize:textSize
|
45
|
2201 |
dataAddr:dataAddr dataSize:dataSize
|
20
|
2202 |
|
|
2203 |
"the object in aFileName must have been linked for
|
|
2204 |
absolute address textAddr/dataAddr (using ld -A).
|
|
2205 |
Load the contents from the file. Memory must have previously
|
|
2206 |
been allocated using ExternalBytes."
|
|
2207 |
|
|
2208 |
%{ /* NOCONTEXT */
|
|
2209 |
#ifdef HAS_DL
|
|
2210 |
/*
|
|
2211 |
* not needed, if dynamic link facilities exist
|
|
2212 |
*/
|
|
2213 |
#else /* no DL-support */
|
|
2214 |
if (! __isString(aFileName)) {
|
45
|
2215 |
RETURN ( nil );
|
20
|
2216 |
}
|
|
2217 |
|
|
2218 |
# if defined(A_DOT_OUT) && !defined(ELF)
|
|
2219 |
# if !defined(sco) && !defined(isc)
|
|
2220 |
{
|
98
|
2221 |
char *fname = (char *) __stringVal(aFileName);
|
45
|
2222 |
unsigned taddr, daddr;
|
|
2223 |
unsigned tsize, dsize;
|
|
2224 |
unsigned toffset = 0;
|
|
2225 |
unsigned doffset = 0;
|
|
2226 |
int fd;
|
|
2227 |
struct exec header;
|
|
2228 |
char *cp;
|
|
2229 |
int bssCount;
|
|
2230 |
unsigned magic = OMAGIC;
|
|
2231 |
int nread;
|
20
|
2232 |
|
60
|
2233 |
taddr = __isSmallInteger(textAddr) ? (unsigned) _intVal(textAddr) : 0;
|
|
2234 |
daddr = __isSmallInteger(dataAddr) ? (unsigned) _intVal(dataAddr) : 0;
|
|
2235 |
tsize = __isSmallInteger(textSize) ? _intVal(textSize) : 0;
|
|
2236 |
dsize = __isSmallInteger(dataSize) ? _intVal(dataSize) : 0;
|
20
|
2237 |
|
45
|
2238 |
if ((fd = open(fname, 0)) < 0) {
|
|
2239 |
fprintf(stderr, "cannot open <%s>\n", fname);
|
|
2240 |
RETURN ( nil );
|
|
2241 |
}
|
|
2242 |
if (read(fd, &header, sizeof(header)) != sizeof(header)) {
|
|
2243 |
fprintf(stderr, "cannot read header of <%s>\n", fname);
|
|
2244 |
close(fd);
|
|
2245 |
RETURN ( nil );
|
|
2246 |
}
|
|
2247 |
if (N_MAGIC(header) != magic) {
|
|
2248 |
fprintf(stderr, "header is (0%o) %x should be (0%o) %x\n",
|
|
2249 |
N_MAGIC(header), N_MAGIC(header),
|
|
2250 |
magic, magic);
|
|
2251 |
close(fd);
|
|
2252 |
RETURN ( nil );
|
|
2253 |
}
|
20
|
2254 |
|
45
|
2255 |
/*
|
|
2256 |
* some linkers produce a huge output file, with zeros up to the
|
|
2257 |
* real code ... - thats what toffset, doffset are for.
|
|
2258 |
*/
|
20
|
2259 |
# if defined(sinix) && defined(BSD)
|
45
|
2260 |
toffset = N_TXTADDR(header);
|
|
2261 |
doffset = toffset + taddr + tsize /* - 0x800 */;
|
|
2262 |
daddr = taddr + tsize;
|
20
|
2263 |
# else
|
|
2264 |
# if defined(mips) && defined(ultrix)
|
45
|
2265 |
toffset = N_TXTOFF(header.ex_f, header.ex_o);
|
|
2266 |
doffset = toffset + tsize;
|
|
2267 |
daddr = taddr + tsize;
|
20
|
2268 |
# else
|
|
2269 |
# if defined(N_TXTOFF)
|
45
|
2270 |
toffset = N_TXTOFF(header);
|
|
2271 |
doffset = N_DATOFF(header);
|
|
2272 |
daddr = taddr + tsize;
|
20
|
2273 |
# else
|
45
|
2274 |
fprintf(stderr, "dont know text/data offsets in objectfile\n");
|
|
2275 |
RETURN ( nil );
|
20
|
2276 |
# endif
|
|
2277 |
# endif
|
|
2278 |
# endif
|
|
2279 |
|
|
2280 |
# ifdef SUPERDEBUG
|
45
|
2281 |
printf("toffs:%x taddr:%x tsize:%d doffs:%x daddr:%x dsize:%d\n",
|
|
2282 |
toffset, taddr, tsize, doffset,daddr, dsize);
|
20
|
2283 |
# endif
|
|
2284 |
|
45
|
2285 |
if (lseek(fd, (long)toffset, 0) < 0) {
|
|
2286 |
fprintf(stderr, "cannot seek to text\n");
|
|
2287 |
close(fd);
|
|
2288 |
RETURN ( nil );
|
|
2289 |
}
|
|
2290 |
if ((nread = read(fd, taddr, tsize)) != tsize) {
|
|
2291 |
perror("read");
|
|
2292 |
fprintf(stderr, "cannot read text wanted:%d got:%d\n", tsize, nread);
|
|
2293 |
close(fd);
|
|
2294 |
RETURN ( nil );
|
|
2295 |
}
|
20
|
2296 |
|
|
2297 |
# ifdef SUPERDEBUG
|
45
|
2298 |
printf("1st bytes of text: %02x %02x %02x %02x\n",
|
|
2299 |
*((char *)taddr) & 0xFF, *((char *)taddr+1) & 0xFF,
|
|
2300 |
*((char *)taddr+2) & 0xFF, *((char *)taddr+3) & 0xFF);
|
20
|
2301 |
# endif
|
|
2302 |
|
45
|
2303 |
if (dsize) {
|
|
2304 |
if (lseek(fd, (long)doffset, 0) < 0) {
|
|
2305 |
fprintf(stderr, "cannot seek to data\n");
|
|
2306 |
close(fd);
|
|
2307 |
RETURN ( nil );
|
|
2308 |
}
|
20
|
2309 |
|
45
|
2310 |
if (read(fd, daddr, dsize) != dsize) {
|
|
2311 |
fprintf(stderr, "cannot read data\n");
|
|
2312 |
close(fd);
|
|
2313 |
RETURN ( nil );
|
|
2314 |
}
|
20
|
2315 |
# ifdef SUPERDEBUG
|
45
|
2316 |
{
|
|
2317 |
char *ptr;
|
|
2318 |
int i;
|
20
|
2319 |
|
45
|
2320 |
ptr = (char *)daddr;
|
|
2321 |
fprintf(stderr, "bytes of data (at %x):\n", ptr);
|
|
2322 |
for (i=dsize; i>0; i--, ptr++)
|
|
2323 |
printf("%02x ", *ptr & 0xFF);
|
|
2324 |
}
|
20
|
2325 |
# endif
|
45
|
2326 |
}
|
|
2327 |
close(fd);
|
20
|
2328 |
|
|
2329 |
# ifdef NOTDEF
|
45
|
2330 |
if (header.a_bss != 0) {
|
|
2331 |
fprintf(stderr, "warning: bss not empty\n");
|
|
2332 |
cp = ((char *)daddr) + header.a_data;
|
|
2333 |
for (bssCount=header.a_bss; bssCount; bssCount--)
|
|
2334 |
*cp++ = 0;
|
|
2335 |
}
|
20
|
2336 |
# endif
|
|
2337 |
}
|
|
2338 |
RETURN ( self );
|
|
2339 |
# endif
|
|
2340 |
# endif
|
|
2341 |
/*
|
|
2342 |
* need support for other headers ... (i.e. coff, elf)
|
|
2343 |
*/
|
|
2344 |
#endif
|
|
2345 |
%}.
|
|
2346 |
^ self error:'objectFile format not supported'
|
|
2347 |
! !
|
|
2348 |
|
0
|
2349 |
ObjectFileLoader initialize!
|