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