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