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:''
|
45
|
15 |
classVariableNames:'MySymbolTable Verbose LastError LoadedObjects'
|
0
|
16 |
poolDictionaries:''
|
|
17 |
category:'System-Compiler'
|
|
18 |
!
|
|
19 |
|
|
20 |
ObjectFileLoader comment:'
|
|
21 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
45
|
22 |
All Rights Reserved
|
28
|
23 |
|
47
|
24 |
$Header: /cvs/stx/stx/libcomp/ObjectFileLoader.st,v 1.11 1994-10-28 03:09:30 claus Exp $
|
0
|
25 |
'!
|
|
26 |
|
47
|
27 |
!ObjectFileLoader class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
32 |
All Rights Reserved
|
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
|
45 |
$Header: /cvs/stx/stx/libcomp/ObjectFileLoader.st,v 1.11 1994-10-28 03:09:30 claus Exp $
|
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
This class knowns how to dynamically load in external object-modules.
|
|
52 |
As to date, this is completely experimental and WITHOUT ANY WARRANTY.
|
|
53 |
It is still being developed and the code below needs cleanup and more
|
|
54 |
robustness.
|
|
55 |
|
|
56 |
There are basically two totally different mechanisms to do this:
|
|
57 |
a) if there exists some dynamic-link facility such as:
|
|
58 |
GNU-DL, dlopen (sparc, SYS5.4), rld_open (NeXT),
|
|
59 |
this is used
|
|
60 |
b) if no such facility exists, the normal linker is used to
|
|
61 |
link the module to text/data address as previously malloced,
|
|
62 |
and the object file loaded into that space.
|
|
63 |
|
|
64 |
Currently, not all mechanisms work fully satisfying.
|
|
65 |
For example, the sun dl*-functions do an exit on link-errors (which
|
|
66 |
is certainly not what we want here :-(; the NeXT mechanism does not
|
|
67 |
allow for selective unloading (only all or last).
|
|
68 |
The only really useful package is the GNU-dl package, which is only
|
|
69 |
available for a.out file formats. (i.e. only linux people can use
|
|
70 |
it at this time).
|
|
71 |
"
|
|
72 |
! !
|
|
73 |
|
|
74 |
!ObjectFileLoader class primitiveDefinitions!
|
|
75 |
|
0
|
76 |
%{
|
15
|
77 |
/*
|
|
78 |
* by default, use whatever the system provides
|
|
79 |
*/
|
|
80 |
#ifdef sunos
|
|
81 |
# define SUN_DL
|
20
|
82 |
# define HAS_DL
|
15
|
83 |
#endif
|
|
84 |
|
0
|
85 |
#ifdef NeXT
|
15
|
86 |
# define NEXT_DL
|
20
|
87 |
# define HAS_DL
|
15
|
88 |
#endif
|
|
89 |
|
|
90 |
#ifdef SYSV4
|
|
91 |
# define SYSV4_DL
|
20
|
92 |
# define HAS_DL
|
15
|
93 |
#endif
|
|
94 |
|
|
95 |
/*
|
20
|
96 |
* but GNU_DL overwrites this - its better
|
15
|
97 |
*/
|
|
98 |
#ifdef GNU_DL
|
20
|
99 |
# define HAS_DL
|
15
|
100 |
# undef SYSV4_DL
|
|
101 |
# undef NEXT_DL
|
|
102 |
# undef SUN_DL
|
|
103 |
#endif
|
|
104 |
|
|
105 |
#ifdef NEXT_DL
|
0
|
106 |
# ifndef _RLD_H_
|
|
107 |
# define _RLD_H_
|
|
108 |
# include <rld.h>
|
|
109 |
# endif
|
15
|
110 |
#endif /* NEXT_DL */
|
|
111 |
|
20
|
112 |
#include <stdio.h>
|
|
113 |
|
|
114 |
/*
|
|
115 |
* if no dynamic link facilities, do it the hard way ...
|
|
116 |
*/
|
|
117 |
#ifndef HAS_DL
|
|
118 |
|
|
119 |
# ifdef A_DOT_OUT
|
|
120 |
# include <a.out.h>
|
|
121 |
# ifndef N_MAGIC
|
|
122 |
# if defined(sinix) && defined(BSD)
|
|
123 |
# define N_MAGIC(hdr) (hdr.a_magic & 0xFFFF)
|
|
124 |
# else
|
|
125 |
# define N_MAGIC(hdr) (hdr.a_magic)
|
|
126 |
# endif
|
|
127 |
# endif
|
|
128 |
# endif /* a.out */
|
|
129 |
|
|
130 |
# ifdef COFF
|
|
131 |
# ifdef mips
|
|
132 |
# include <sys/exec.h>
|
|
133 |
# else
|
|
134 |
# include <a.out.h>
|
|
135 |
# endif
|
|
136 |
# endif /* coff */
|
|
137 |
|
|
138 |
# ifdef ELF
|
|
139 |
# include <elf.h>
|
|
140 |
# endif /* elf */
|
|
141 |
|
|
142 |
#endif /* not HAS_DL */
|
|
143 |
|
28
|
144 |
#ifdef SYSV4_DL
|
|
145 |
# include <dlfcn.h>
|
|
146 |
# define dlcfn_h
|
|
147 |
#endif
|
|
148 |
|
45
|
149 |
#ifdef GNU_DL
|
|
150 |
# ifndef dld_h
|
|
151 |
# include "dld.h"
|
|
152 |
# define dld_h
|
|
153 |
# endif
|
|
154 |
#endif
|
0
|
155 |
%}
|
20
|
156 |
! !
|
|
157 |
|
0
|
158 |
!ObjectFileLoader class methodsFor:'initialization'!
|
|
159 |
|
|
160 |
initialize
|
|
161 |
"name of object file, where initial symbol table is found"
|
|
162 |
|
10
|
163 |
MySymbolTable := 'smalltalk'.
|
45
|
164 |
Verbose := false.
|
0
|
165 |
!
|
|
166 |
|
|
167 |
verbose:aBoolean
|
|
168 |
"turn on/off debug traces"
|
|
169 |
|
10
|
170 |
Verbose := aBoolean
|
0
|
171 |
|
|
172 |
"ObjectFileLoader verbose:true"
|
|
173 |
! !
|
|
174 |
|
|
175 |
!ObjectFileLoader class methodsFor:'command defaults'!
|
|
176 |
|
|
177 |
needSeparateIDSpaces
|
20
|
178 |
"return true, if we need separate I and D spaces.
|
|
179 |
This is only needed if no dynamic-link facilitiy exists."
|
0
|
180 |
|
|
181 |
|os cpu|
|
|
182 |
|
|
183 |
os := OperatingSystem getSystemType.
|
|
184 |
cpu := OperatingSystem getCPUType.
|
|
185 |
|
|
186 |
(os = 'sunos') ifTrue:[
|
45
|
187 |
(cpu = 'sparc') ifTrue:[ ^ true ]
|
0
|
188 |
].
|
6
|
189 |
(os = 'linux') ifTrue:[ ^ false ].
|
|
190 |
|
20
|
191 |
'dont know if we need sepId - assume no' errorPrintNL.
|
6
|
192 |
^ false
|
0
|
193 |
!
|
|
194 |
|
|
195 |
absLd:file text:textAddr data:dataAddr
|
20
|
196 |
"this should return a string to link file.o to absolute address.
|
|
197 |
This is only needed if no dynamic-link facilitiy exists."
|
0
|
198 |
|
|
199 |
|os cpu|
|
|
200 |
|
|
201 |
os := OperatingSystem getSystemType.
|
|
202 |
cpu := OperatingSystem getCPUType.
|
|
203 |
(os = 'sunos') ifTrue:[
|
45
|
204 |
(cpu = 'sparc') ifTrue:[
|
6
|
205 |
" "
|
45
|
206 |
^ ('ld -A ' , MySymbolTable , ' -x -Bstatic' ,
|
|
207 |
' -Ttext ' , (textAddr printStringRadix:16) ,
|
|
208 |
' -Tdata ' , (dataAddr printStringRadix:16) , ' ' , file)
|
6
|
209 |
" "
|
0
|
210 |
"
|
45
|
211 |
^ ('ld -A ' , MySymbolTable , ' -T ',
|
|
212 |
(textAddr printStringRadix:16),
|
|
213 |
' -N -x ' , file)
|
6
|
214 |
"
|
45
|
215 |
]
|
0
|
216 |
].
|
|
217 |
"
|
|
218 |
(os = 'ultrix') ifTrue:[
|
45
|
219 |
(cpu = 'mips') ifTrue:[
|
|
220 |
^ ('ld -A ' , MySymbolTable , ' -x -N -T ' , (textAddr printStringRadix:16) , ' ' , file)
|
|
221 |
]
|
0
|
222 |
].
|
|
223 |
"
|
6
|
224 |
(os = 'linux') ifTrue:[
|
45
|
225 |
^ ('ld -A ' , MySymbolTable ,
|
|
226 |
' -x -N ',
|
|
227 |
' -Ttext ' , (textAddr printStringRadix:16) ,
|
|
228 |
' -Tdata ' , (dataAddr printStringRadix:16) , ' ' , file)
|
6
|
229 |
].
|
0
|
230 |
self error:'do not know how to link absolute'
|
6
|
231 |
!
|
|
232 |
|
|
233 |
absLd:file text:textAddr
|
20
|
234 |
"this should return a string to link file.o to absolute address.
|
|
235 |
This is only needed if no dynamic-link facilitiy exists."
|
6
|
236 |
|
|
237 |
|os cpu|
|
|
238 |
|
|
239 |
os := OperatingSystem getSystemType.
|
|
240 |
cpu := OperatingSystem getCPUType.
|
|
241 |
(os = 'sunos') ifTrue:[
|
45
|
242 |
(cpu = 'sparc') ifTrue:[
|
|
243 |
^ ('ld -A ' , MySymbolTable , ' -T ',
|
|
244 |
(textAddr printStringRadix:16),
|
|
245 |
' -N -x ' , file)
|
6
|
246 |
|
45
|
247 |
]
|
6
|
248 |
].
|
|
249 |
(os = 'linux') ifTrue:[
|
45
|
250 |
^ ('ld -A ' , MySymbolTable ,
|
|
251 |
' -x -N -Ttext ' , (textAddr printStringRadix:16) , ' ' , file)
|
6
|
252 |
].
|
|
253 |
self error:'do not know how to link absolute'
|
28
|
254 |
!
|
6
|
255 |
|
28
|
256 |
nm:file
|
|
257 |
"this should return a string to list the namelist of file"
|
|
258 |
|
|
259 |
|os cpu|
|
|
260 |
|
|
261 |
os := OperatingSystem getSystemType.
|
|
262 |
(os = 'iris') ifTrue:[
|
45
|
263 |
^ 'nm -B ' , file
|
28
|
264 |
].
|
|
265 |
^ 'nm ' , file
|
0
|
266 |
! !
|
|
267 |
|
|
268 |
!ObjectFileLoader class methodsFor:'loading objects'!
|
|
269 |
|
|
270 |
loadFile:oFile with:librariesString
|
20
|
271 |
"load in an object files code, linking in libraries.
|
|
272 |
This is only needed if no dynamic link facility exists."
|
0
|
273 |
|
20
|
274 |
|tmpOfile errStream errors errText handle pid cmd|
|
0
|
275 |
|
|
276 |
pid := OperatingSystem getProcessId printString.
|
|
277 |
tmpOfile := '/tmp/stc_ld' , pid.
|
20
|
278 |
cmd := 'ld -o ', tmpOfile, ' -r ' , oFile , ' ' , librariesString , '>/tmp/out 2>/tmp/err'.
|
10
|
279 |
Verbose ifTrue:[
|
45
|
280 |
('executing: ld -o ', cmd) errorPrintNL
|
0
|
281 |
].
|
20
|
282 |
(OperatingSystem executeCommand:cmd) ifFalse:[
|
45
|
283 |
errStream := FileStream oldFileNamed:'/tmp/err'.
|
|
284 |
errStream isNil ifTrue:[
|
|
285 |
self notify:'errors during link.'
|
|
286 |
] ifFalse:[
|
|
287 |
errors := errStream contents.
|
|
288 |
errText := errors asText.
|
|
289 |
(errText size > 20) ifTrue:[
|
|
290 |
errText grow:20.
|
|
291 |
errText add:'... '.
|
|
292 |
errors := errText
|
|
293 |
].
|
|
294 |
OperatingSystem executeCommand:'rm /tmp/err /tmp/out'.
|
|
295 |
self notify:('link errors:\\' , errors asString) withCRs
|
|
296 |
].
|
|
297 |
^ false
|
0
|
298 |
].
|
20
|
299 |
handle := self loadFile:tmpOfile.
|
0
|
300 |
OperatingSystem executeCommand:('rm ' , tmpOfile).
|
20
|
301 |
^ handle
|
0
|
302 |
!
|
|
303 |
|
|
304 |
loadFile:oFile
|
20
|
305 |
"load in an object file - return a handle or nil.
|
45
|
306 |
This is only needed if no dynamic link facility exists.
|
|
307 |
It allocates some memory for text and data, calls for the linker
|
|
308 |
to relocate the oFile to that address and loads the sections."
|
0
|
309 |
|
20
|
310 |
|unixCommand errStream errors errText
|
|
311 |
text data textSize dataSize dataAddr textAddr newTextSize newDataSize|
|
0
|
312 |
|
|
313 |
"find out, how much memory we need"
|
|
314 |
|
20
|
315 |
textSize := self textSizeOf:oFile.
|
0
|
316 |
textSize isNil ifTrue:[
|
45
|
317 |
'bad text-size in object file' errorPrintNL.
|
|
318 |
^ nil
|
0
|
319 |
].
|
10
|
320 |
Verbose ifTrue:[
|
45
|
321 |
('text-size: ' , (textSize printStringRadix:16)) errorPrintNL
|
0
|
322 |
].
|
|
323 |
|
20
|
324 |
dataSize := self dataSizeOf:oFile.
|
0
|
325 |
dataSize isNil ifTrue:[
|
45
|
326 |
'bad data-size in object file' errorPrintNL.
|
|
327 |
^ nil
|
0
|
328 |
].
|
|
329 |
|
10
|
330 |
Verbose ifTrue:[
|
45
|
331 |
('data-size: ' , (dataSize printStringRadix:16)) errorPrintNL
|
0
|
332 |
].
|
|
333 |
|
|
334 |
"allocate some memory for text and some for data;
|
|
335 |
then call linker to link the file to those addresses"
|
|
336 |
|
|
337 |
self needSeparateIDSpaces ifTrue:[
|
45
|
338 |
text := ExternalBytes newForText:textSize.
|
|
339 |
text isNil ifTrue:[
|
|
340 |
'cannot allocate memory for text' errorPrintNL.
|
|
341 |
^ nil
|
|
342 |
].
|
0
|
343 |
|
45
|
344 |
Verbose ifTrue:[
|
|
345 |
('text: ' , (text address printStringRadix:16)) errorPrintNL
|
|
346 |
].
|
0
|
347 |
|
45
|
348 |
(dataSize ~~ 0) ifTrue:[
|
|
349 |
data := ExternalBytes newForData:dataSize.
|
|
350 |
(data isNil) ifTrue:[
|
|
351 |
'cannot allocate memory for data' errorPrintNL.
|
|
352 |
text notNil ifTrue:[text free].
|
|
353 |
^ nil
|
|
354 |
].
|
|
355 |
Verbose ifTrue:[
|
|
356 |
('data: ' , (data address printStringRadix:16)) errorPrintNL
|
|
357 |
]
|
|
358 |
].
|
|
359 |
dataSize == 0 ifTrue:[
|
|
360 |
unixCommand := (self absLd:oFile text:text address) , ' >/tmp/out 2>/tmp/err'.
|
|
361 |
] ifFalse:[
|
|
362 |
unixCommand := (self absLd:oFile text:text address data:data address)
|
|
363 |
, ' >/tmp/out 2>/tmp/err'.
|
|
364 |
]
|
0
|
365 |
] ifFalse:[
|
45
|
366 |
text := ExternalBytes newForText:(textSize + dataSize).
|
|
367 |
text isNil ifTrue:[
|
|
368 |
'cannot allocate memory for text+data' errorPrintNL.
|
|
369 |
^ nil
|
|
370 |
].
|
|
371 |
Verbose ifTrue:[
|
|
372 |
('addr: ' , (text address printStringRadix:16)) errorPrintNL
|
|
373 |
].
|
|
374 |
unixCommand := (self absLd:oFile text:text address) , ' >/tmp/out 2>/tmp/err'.
|
0
|
375 |
].
|
|
376 |
|
10
|
377 |
Verbose ifTrue:[
|
45
|
378 |
('executing: ' , unixCommand) errorPrintNL
|
0
|
379 |
].
|
|
380 |
|
19
|
381 |
'linking ...' printNewline.
|
0
|
382 |
(OperatingSystem executeCommand:unixCommand) ifFalse: [
|
45
|
383 |
errStream := FileStream oldFileNamed:'/tmp/err'.
|
|
384 |
errStream notNil ifTrue:[
|
|
385 |
errors := errStream contents.
|
|
386 |
errText := errors asText.
|
|
387 |
(errText size > 20) ifTrue:[
|
|
388 |
errText grow:20.
|
|
389 |
errText add:'... '.
|
|
390 |
errors := errText
|
|
391 |
].
|
|
392 |
OperatingSystem executeCommand:'rm /tmp/err /tmp/out'.
|
|
393 |
self notify:('link errors:\\' , errors asString) withCRs
|
|
394 |
].
|
|
395 |
'link unsuccessful.' errorPrintNL.
|
|
396 |
text notNil ifTrue:[text free].
|
|
397 |
data notNil ifTrue:[data free].
|
|
398 |
^ nil
|
0
|
399 |
].
|
|
400 |
|
20
|
401 |
'link successful' errorPrintNL.
|
0
|
402 |
|
|
403 |
OperatingSystem executeCommand:'rm /tmp/err /tmp/out'.
|
|
404 |
|
6
|
405 |
"find out, if space needs have changed after link (they do so on some machines)"
|
|
406 |
|
20
|
407 |
newTextSize := self textSizeOf:'a.out'.
|
6
|
408 |
newTextSize isNil ifTrue:[
|
45
|
409 |
'bad new-text-size in a.out object file' errorPrintNL.
|
|
410 |
text notNil ifTrue:[text free].
|
|
411 |
data notNil ifTrue:[data free].
|
|
412 |
^ nil
|
6
|
413 |
].
|
10
|
414 |
Verbose ifTrue:[
|
45
|
415 |
('new-text-size: ' , (newTextSize printStringRadix:16)) errorPrintNL
|
6
|
416 |
].
|
|
417 |
|
20
|
418 |
newDataSize := self dataSizeOf:'a.out'.
|
6
|
419 |
newDataSize isNil ifTrue:[
|
45
|
420 |
'bad new-data-size in a.out object file' errorPrintNL.
|
|
421 |
text notNil ifTrue:[text free].
|
|
422 |
data notNil ifTrue:[data free].
|
|
423 |
^ nil
|
6
|
424 |
].
|
|
425 |
|
10
|
426 |
Verbose ifTrue:[
|
45
|
427 |
('new-data-size: ' , (newDataSize printStringRadix:16)) errorPrintNL
|
6
|
428 |
].
|
|
429 |
|
|
430 |
"if size has changed, do it again"
|
|
431 |
|
|
432 |
((newTextSize ~~ textSize) or:[newDataSize ~~ dataSize]) ifTrue:[
|
|
433 |
|
45
|
434 |
'size changed after link - do it again' printNewline.
|
6
|
435 |
|
45
|
436 |
text notNil ifTrue:[text free. text := nil].
|
|
437 |
data notNil ifTrue:[data free. data := nil].
|
|
438 |
textSize := newTextSize.
|
|
439 |
dataSize := newDataSize.
|
6
|
440 |
|
45
|
441 |
self needSeparateIDSpaces ifTrue:[
|
|
442 |
text := ExternalBytes newForText:textSize.
|
|
443 |
text isNil ifTrue:[
|
|
444 |
'cannot allocate memory for new text' errorPrintNL.
|
|
445 |
^ nil
|
|
446 |
].
|
6
|
447 |
|
45
|
448 |
Verbose ifTrue:[
|
|
449 |
('new text: ' , (text address printStringRadix:16)) errorPrintNL
|
|
450 |
].
|
6
|
451 |
|
45
|
452 |
(dataSize ~~ 0) ifTrue:[
|
|
453 |
data := ExternalBytes newForData:dataSize.
|
|
454 |
(data isNil) ifTrue:[
|
|
455 |
'cannot allocate memory for new data' errorPrintNL.
|
|
456 |
text notNil ifTrue:[text free].
|
|
457 |
^ nil
|
|
458 |
].
|
|
459 |
Verbose ifTrue:[
|
|
460 |
('new data: ' , (data address printStringRadix:16)) errorPrintNL
|
|
461 |
]
|
|
462 |
].
|
6
|
463 |
|
45
|
464 |
dataSize == 0 ifTrue:[
|
|
465 |
unixCommand := (self absLd:oFile text:text address) , ' >/tmp/out 2>/tmp/err'.
|
|
466 |
] ifFalse:[
|
|
467 |
unixCommand := (self absLd:oFile text:text address data:data address)
|
|
468 |
, ' >/tmp/out 2>/tmp/err'.
|
|
469 |
]
|
|
470 |
] ifFalse:[
|
|
471 |
text := ExternalBytes newForText:(textSize + dataSize).
|
|
472 |
text isNil ifTrue:[
|
|
473 |
'cannot allocate memory for new text' errorPrintNL.
|
|
474 |
^ nil
|
|
475 |
].
|
|
476 |
Verbose ifTrue:[
|
|
477 |
('new text+data: ' , (text address printStringRadix:16)) errorPrintNL
|
|
478 |
].
|
|
479 |
unixCommand := (self absLd:oFile text:text address) , ' >/tmp/out 2>/tmp/err'.
|
|
480 |
].
|
6
|
481 |
|
45
|
482 |
Verbose ifTrue:[
|
|
483 |
('executing: ' , unixCommand) errorPrintNL
|
|
484 |
].
|
6
|
485 |
|
45
|
486 |
'linking ...' errorPrintNL.
|
|
487 |
(OperatingSystem executeCommand:unixCommand) ifFalse: [
|
|
488 |
errStream := FileStream oldFileNamed:'/tmp/err'.
|
|
489 |
errStream notNil ifTrue:[
|
|
490 |
errors := errStream contents.
|
|
491 |
errText := errors asText.
|
|
492 |
(errText size > 20) ifTrue:[
|
|
493 |
errText grow:20.
|
|
494 |
errText add:'... '.
|
|
495 |
errors := errText
|
|
496 |
].
|
|
497 |
OperatingSystem executeCommand:'rm /tmp/err /tmp/out'.
|
|
498 |
self notify:('link errors:\\' , errors asString) withCRs
|
|
499 |
].
|
|
500 |
'link unsuccessful.' errorPrintNL.
|
|
501 |
text notNil ifTrue:[text free].
|
|
502 |
data notNil ifTrue:[data free].
|
|
503 |
^ nil
|
|
504 |
].
|
6
|
505 |
|
45
|
506 |
'link successful' errorPrintNL.
|
6
|
507 |
|
45
|
508 |
OperatingSystem executeCommand:'rm /tmp/err /tmp/out'.
|
6
|
509 |
|
45
|
510 |
"check again for size change - should not happen"
|
6
|
511 |
|
45
|
512 |
newTextSize := self textSizeOf:'a.out'.
|
|
513 |
newTextSize isNil ifTrue:[
|
|
514 |
'bad text-size in a.out object file' errorPrintNL.
|
|
515 |
text notNil ifTrue:[text free].
|
|
516 |
data notNil ifTrue:[data free].
|
|
517 |
^ nil
|
|
518 |
].
|
|
519 |
Verbose ifTrue:[
|
|
520 |
('new-text-size: ' , (newTextSize printStringRadix:16)) errorPrintNL
|
|
521 |
].
|
6
|
522 |
|
45
|
523 |
newDataSize := self dataSizeOf:'a.out'.
|
|
524 |
newDataSize isNil ifTrue:[
|
|
525 |
'bad data-size in object file' errorPrintNL.
|
|
526 |
text notNil ifTrue:[text free].
|
|
527 |
data notNil ifTrue:[data free].
|
|
528 |
^ nil
|
|
529 |
].
|
6
|
530 |
|
45
|
531 |
Verbose ifTrue:[
|
|
532 |
('new-data-size: ' , (newDataSize printStringRadix:16)) errorPrintNL
|
|
533 |
].
|
6
|
534 |
|
45
|
535 |
((newTextSize ~~ textSize) or:[newDataSize ~~ dataSize]) ifTrue:[
|
|
536 |
'size changed again - I give up' errorPrintNL.
|
|
537 |
text notNil ifTrue:[text free].
|
|
538 |
data notNil ifTrue:[data free].
|
|
539 |
^ nil
|
|
540 |
].
|
6
|
541 |
].
|
|
542 |
|
0
|
543 |
"only thing left to do is to load in text at textAddr and
|
|
544 |
data at dataAddr ... "
|
|
545 |
|
6
|
546 |
text notNil ifTrue:[
|
45
|
547 |
textAddr := text address
|
6
|
548 |
] ifFalse:[
|
45
|
549 |
textAddr := nil
|
6
|
550 |
].
|
|
551 |
data notNil ifTrue:[
|
45
|
552 |
dataAddr := data address
|
6
|
553 |
] ifFalse:[
|
45
|
554 |
dataAddr := nil
|
6
|
555 |
].
|
|
556 |
|
19
|
557 |
Verbose ifTrue:[
|
45
|
558 |
textAddr notNil ifTrue:[
|
|
559 |
('loading ' , textSize printString , ' bytes at ' , (textAddr printStringRadix:16)) errorPrintNL.
|
|
560 |
].
|
|
561 |
dataAddr notNil ifTrue:[
|
|
562 |
('loading ' , dataSize printString , ' bytes at ' , (dataAddr printStringRadix:16)) errorPrintNL.
|
|
563 |
].
|
19
|
564 |
].
|
6
|
565 |
|
20
|
566 |
(self loadObjectFile:'a.out'
|
45
|
567 |
textAddr:textAddr textSize:textSize
|
|
568 |
dataAddr:dataAddr dataSize:dataSize) isNil ifTrue: [
|
|
569 |
'load error' errorPrintNL.
|
|
570 |
text notNil ifTrue:[text free].
|
|
571 |
data notNil ifTrue:[data free].
|
|
572 |
^ nil
|
0
|
573 |
].
|
|
574 |
|
20
|
575 |
'dynamic load successful' errorPrintNL.
|
0
|
576 |
|
|
577 |
OperatingSystem executeCommand:'mv a.out SymbolTable'.
|
10
|
578 |
MySymbolTable := 'SymbolTable'.
|
45
|
579 |
^ (Array with:text with:data)
|
0
|
580 |
! !
|
|
581 |
|
|
582 |
!ObjectFileLoader class methodsFor:'dynamic class loading'!
|
|
583 |
|
|
584 |
loadClass:aClassName fromObjectFile:aFileName
|
|
585 |
"load a compiled class (.o-file) into the image"
|
|
586 |
|
20
|
587 |
|handle initAddr symName newClass list moreHandles|
|
0
|
588 |
|
|
589 |
handle := self openDynamicObject:aFileName.
|
|
590 |
handle isNil ifTrue:[
|
45
|
591 |
Transcript showCr:('openDynamic: ',aFileName,' failed.').
|
|
592 |
^ nil
|
0
|
593 |
].
|
20
|
594 |
|
|
595 |
"
|
|
596 |
get the Init-function; let the class install itself
|
|
597 |
"
|
19
|
598 |
symName := '_' , aClassName , '_Init'.
|
20
|
599 |
initAddr := self getFunction:symName from:handle.
|
0
|
600 |
initAddr isNil ifTrue:[
|
45
|
601 |
"try with added underscore"
|
|
602 |
symName := '__' , aClassName , '_Init'.
|
|
603 |
initAddr := self getFunction:symName from:handle.
|
20
|
604 |
].
|
|
605 |
|
|
606 |
"
|
|
607 |
if there are any undefined symbols, we may have to load more
|
|
608 |
"
|
|
609 |
list := self getListOfUndefinedSymbolsFrom:handle.
|
|
610 |
list notNil ifTrue:[
|
45
|
611 |
moreHandles := self loadModulesFromListOfUndefined:list.
|
20
|
612 |
|
45
|
613 |
"
|
|
614 |
now, try again
|
|
615 |
"
|
|
616 |
symName := '_' , aClassName , '_Init'.
|
|
617 |
initAddr := self getFunction:symName from:handle.
|
|
618 |
initAddr isNil ifTrue:[
|
|
619 |
"try with added underscore"
|
|
620 |
symName := '__' , aClassName , '_Init'.
|
|
621 |
initAddr := self getFunction:symName from:handle.
|
|
622 |
].
|
0
|
623 |
].
|
20
|
624 |
|
|
625 |
initAddr notNil ifTrue:[
|
45
|
626 |
Verbose ifTrue:[
|
|
627 |
Transcript showCr:'calling init at: ' , (initAddr printStringRadix:16)
|
|
628 |
].
|
|
629 |
self callInitFunctionAt:initAddr.
|
|
630 |
(Symbol hasInterned:aClassName) ifTrue:[
|
|
631 |
newClass := Smalltalk at:aClassName asSymbol ifAbsent:[nil].
|
|
632 |
newClass notNil ifTrue:[
|
|
633 |
newClass initialize.
|
|
634 |
"force cache flush"
|
|
635 |
Smalltalk at:aClassName asSymbol put:newClass.
|
|
636 |
Smalltalk changed.
|
|
637 |
].
|
|
638 |
] ifFalse:[
|
|
639 |
'LOADER: class ' errorPrintNL. aClassName errorPrintNL.
|
|
640 |
' did not define itself' errorPrintNL
|
|
641 |
"
|
|
642 |
do not unload - could have installed its methods ...
|
|
643 |
"
|
|
644 |
].
|
|
645 |
^ newClass
|
20
|
646 |
].
|
|
647 |
|
|
648 |
Verbose ifTrue:[
|
45
|
649 |
Transcript showCr:('no symbol: ', symName,' in ',aFileName).
|
20
|
650 |
].
|
|
651 |
|
|
652 |
"
|
|
653 |
unload
|
|
654 |
"
|
|
655 |
moreHandles notNil ifTrue:[
|
45
|
656 |
self closeAllDynamicObjects:moreHandles.
|
20
|
657 |
].
|
|
658 |
self closeDynamicObject:handle.
|
|
659 |
^ nil
|
0
|
660 |
|
|
661 |
"ObjectFileLoader loadClass:'Tetris' fromObjectFile:'../clients/Tetris/Tetris.o'"
|
|
662 |
"ObjectFileLoader loadClass:'TetrisBlock' fromObjectFile:'../clients/Tetris/TBlock.o'"
|
20
|
663 |
"ObjectFileLoader loadClass:'Foo' fromObjectFile:'classList.o'"
|
0
|
664 |
!
|
|
665 |
|
|
666 |
loadObjectFile:aFileName
|
20
|
667 |
"load an object file (.o-file) into the image;
|
37
|
668 |
the class name is not needed (multiple definitions may be in the file).
|
|
669 |
Return false on error, true if ok."
|
0
|
670 |
|
28
|
671 |
|handle initAddr symName className newClass list initNames|
|
0
|
672 |
|
|
673 |
handle := self openDynamicObject:aFileName.
|
|
674 |
handle isNil ifTrue:[
|
45
|
675 |
Transcript showCr:('openDynamic: ',aFileName,' failed.').
|
|
676 |
^ false
|
0
|
677 |
].
|
19
|
678 |
|
0
|
679 |
className := OperatingSystem baseNameOf:aFileName.
|
|
680 |
(className endsWith:'.o') ifTrue:[
|
45
|
681 |
className := className copyTo:(className size - 2)
|
37
|
682 |
] ifFalse:[
|
45
|
683 |
(className endsWith:'.obj') ifTrue:[
|
|
684 |
className := className copyTo:(className size - 4)
|
|
685 |
] ifFalse:[
|
|
686 |
(className endsWith:'.so') ifTrue:[
|
|
687 |
className := className copyTo:(className size - 3)
|
|
688 |
]
|
|
689 |
]
|
0
|
690 |
].
|
19
|
691 |
|
28
|
692 |
"
|
37
|
693 |
look for explicit init function
|
28
|
694 |
"
|
37
|
695 |
initAddr := self getFunction:(className , '_Init') from:handle.
|
|
696 |
initAddr isNil ifTrue:[
|
45
|
697 |
initAddr := self getFunction:('_' , className , '_Init') from:handle.
|
37
|
698 |
].
|
|
699 |
initAddr notNil ifTrue:[
|
45
|
700 |
self callInitFunctionAt:initAddr.
|
37
|
701 |
] ifFalse:[
|
45
|
702 |
"
|
|
703 |
look for init-function(s)
|
|
704 |
"
|
|
705 |
initNames := self namesMatching:'*_Init' segment:'[tT]' in:aFileName.
|
|
706 |
initNames do:[:aName |
|
|
707 |
initAddr := self getFunction:aName from:handle.
|
|
708 |
initAddr isNil ifTrue:[
|
|
709 |
(aName startsWith:'_') ifTrue:[
|
|
710 |
initAddr := self getFunction:(aName copyFrom:2) from:handle.
|
|
711 |
].
|
|
712 |
].
|
|
713 |
initAddr isNil ifTrue:[
|
|
714 |
Transcript showCr:('no symbol: ',aName,' in ',aFileName).
|
|
715 |
^ nil
|
|
716 |
].
|
|
717 |
Verbose ifTrue:[
|
|
718 |
Transcript showCr:'calling init at:' , (initAddr printStringRadix:16)
|
|
719 |
].
|
|
720 |
self callInitFunctionAt:initAddr.
|
|
721 |
].
|
0
|
722 |
].
|
20
|
723 |
|
37
|
724 |
"/ (Symbol hasInterned:className) ifTrue:[
|
|
725 |
"/ newClass := Smalltalk at:className asSymbol ifAbsent:[nil].
|
|
726 |
"/ newClass notNil ifTrue:[
|
|
727 |
"/ newClass initialize.
|
|
728 |
"/ "force cache flush"
|
|
729 |
"/ Smalltalk at:className asSymbol put:newClass.
|
|
730 |
"/ ].
|
|
731 |
"/ ].
|
|
732 |
|
|
733 |
"
|
|
734 |
really dont know, if it has changed ...
|
|
735 |
"
|
|
736 |
Smalltalk changed.
|
|
737 |
^ true
|
20
|
738 |
!
|
|
739 |
|
|
740 |
loadCPlusPlusObjectFile:aFileName
|
|
741 |
"load a c++ object file (.o-file) into the image"
|
|
742 |
|
|
743 |
|handle initAddr symName className newClass list|
|
|
744 |
|
|
745 |
handle := self openDynamicObject:aFileName.
|
|
746 |
handle isNil ifTrue:[
|
45
|
747 |
Transcript showCr:('openDynamic: ',aFileName,' failed.').
|
|
748 |
^ nil
|
20
|
749 |
].
|
|
750 |
|
28
|
751 |
list := self namesMatching:'__GLOBAL_$I*' segment:'[tT]' in:aFileName.
|
20
|
752 |
list size == 1 ifTrue:[
|
|
753 |
"/ (self isCPlusPlusObject:handle) ifTrue:[
|
45
|
754 |
Verbose ifTrue:[
|
|
755 |
Transcript showCr:'a c++ object file'
|
|
756 |
].
|
|
757 |
"
|
|
758 |
what I would like to get is the CTOR_LIST,
|
|
759 |
and call each function.
|
|
760 |
But dld cannot (currently) handle SET-type symbols, therefore
|
|
761 |
we search (using nm) for all __GLOBAL_$I* syms, get their values
|
|
762 |
and call them each
|
|
763 |
"
|
28
|
764 |
"/ list := self namesMatching:'__GLOBAL_$I*' segment:'[tT]' in:aFileName.
|
20
|
765 |
|
|
766 |
"/ initAddr := self getFunction:'__CTOR_LIST__' from:handle.
|
|
767 |
"/ Verbose ifTrue:[
|
|
768 |
"/ Transcript showCr:'calling CTORs at:' , (initAddr printStringRadix:16)
|
|
769 |
"/ ].
|
|
770 |
|
45
|
771 |
initAddr := self getFunction:list first from:handle.
|
|
772 |
initAddr isNil ifTrue:[
|
|
773 |
"
|
|
774 |
try with added underscore
|
|
775 |
"
|
|
776 |
initAddr := self getFunction:('_' , list first) from:handle.
|
|
777 |
].
|
|
778 |
(initAddr isNil and:[list first startsWith:'_']) ifTrue:[
|
|
779 |
"
|
|
780 |
try with removed underscore
|
|
781 |
"
|
|
782 |
initAddr := self getFunction:(list first copyFrom:2) from:handle.
|
|
783 |
].
|
|
784 |
initAddr isNil ifTrue:[
|
|
785 |
Verbose ifTrue:[
|
|
786 |
Transcript showCr:'no CTOR-func found (' , list first , ')'
|
|
787 |
].
|
|
788 |
self closeDynamicObject:aFileName.
|
|
789 |
^ nil
|
|
790 |
].
|
|
791 |
Verbose ifTrue:[
|
|
792 |
Transcript showCr:'calling CTORs at:' , (initAddr printStringRadix:16)
|
|
793 |
].
|
|
794 |
self callFunctionAt:initAddr forceOld:false arg:0.
|
|
795 |
Verbose ifTrue:[
|
|
796 |
Transcript showCr:'done with CTORs.'
|
|
797 |
].
|
20
|
798 |
|
45
|
799 |
"
|
|
800 |
cannot create a CPlusPlus class automatically (there could be more than
|
|
801 |
one classes in it too ...)
|
|
802 |
"
|
|
803 |
^ handle
|
20
|
804 |
].
|
|
805 |
|
|
806 |
|
|
807 |
Verbose ifTrue:[
|
45
|
808 |
Transcript showCr:'unknown object file'
|
20
|
809 |
].
|
|
810 |
self closeDynamicObject:aFileName.
|
|
811 |
^ nil
|
|
812 |
!
|
|
813 |
|
|
814 |
loadModulesFromListOfUndefined:list
|
|
815 |
"try to figure out what has to be loaded to resolve symbols from list.
|
|
816 |
return a list of handles of loaded objects
|
|
817 |
"
|
|
818 |
|inits classNames moreHandles|
|
|
819 |
|
|
820 |
inits := list select:[:symbol | symbol notNil and:[symbol endsWith:'_Init']].
|
|
821 |
inits notNil ifTrue:[
|
45
|
822 |
classNames := inits collect:[:symbol |
|
|
823 |
(symbol startsWith:'___') ifTrue:[
|
|
824 |
symbol copyFrom:4 to:(symbol size - 5)
|
|
825 |
] ifFalse:[
|
|
826 |
(symbol startsWith:'__') ifTrue:[
|
|
827 |
symbol copyFrom:3 to:(symbol size - 5)
|
|
828 |
] ifFalse:[
|
|
829 |
(symbol startsWith:'_') ifTrue:[
|
|
830 |
symbol copyFrom:2 to:(symbol size - 5)
|
|
831 |
] ifFalse:[
|
|
832 |
symbol
|
|
833 |
]
|
|
834 |
]
|
|
835 |
]
|
|
836 |
].
|
|
837 |
"
|
|
838 |
autoload those classes
|
|
839 |
"
|
|
840 |
classNames do:[:aClassName |
|
|
841 |
|cls|
|
|
842 |
|
|
843 |
(cls := Smalltalk classNamed:aClassName) notNil ifTrue:[
|
|
844 |
'autoloading ' print. aClassName printNL.
|
|
845 |
cls autoload
|
|
846 |
]
|
|
847 |
]
|
20
|
848 |
].
|
|
849 |
^ nil
|
45
|
850 |
!
|
|
851 |
|
|
852 |
unloadObjectFile:aFileName
|
|
853 |
"unload an object file (.o-file) from the image.
|
|
854 |
DANGER ALERT: currently, you have to make sure that no references to
|
|
855 |
objects of this module exist - in future versions, the system will keep
|
|
856 |
track of these. For now, use at your own risk.
|
|
857 |
(especially critical are blocks-functions)."
|
|
858 |
|
|
859 |
|handle|
|
|
860 |
|
|
861 |
LoadedObjects notNil ifTrue:[
|
|
862 |
handle := LoadedObjects at:aFileName
|
|
863 |
].
|
|
864 |
handle isNil ifTrue:[
|
|
865 |
self error:'this file was not loaded dynamically'.
|
|
866 |
^ self
|
|
867 |
].
|
|
868 |
|
|
869 |
"/call the deInit-function ...
|
|
870 |
|
|
871 |
self closeDynamicObject:aFileName
|
|
872 |
!
|
|
873 |
|
|
874 |
unloadAllObjectFiles
|
|
875 |
"unload all dynamically loaded object files from the image.
|
|
876 |
see DANGER ALERT in ObjectFileLoader>>unloadObjectFile:"
|
|
877 |
|
|
878 |
|handle|
|
|
879 |
|
|
880 |
LoadedObjects notNil ifTrue:[
|
|
881 |
LoadedObjects keysDo:[:aFileName |
|
|
882 |
self unloadObjectFile:aFileName
|
|
883 |
]
|
|
884 |
].
|
|
885 |
|
|
886 |
"
|
|
887 |
ObjectFileLoader unloadAllObjectFiles
|
|
888 |
"
|
0
|
889 |
! !
|
|
890 |
|
|
891 |
!ObjectFileLoader class methodsFor:'dynamic object access'!
|
|
892 |
|
|
893 |
openDynamicObject:pathName
|
20
|
894 |
"open an object-file (load/map into my address space).
|
0
|
895 |
Return a non-nil handle if ok, nil otherwise.
|
20
|
896 |
No bindings are done - only a pure load is performed.
|
|
897 |
This function is not supported on all architectures.
|
|
898 |
"
|
0
|
899 |
|
15
|
900 |
|handle|
|
6
|
901 |
|
20
|
902 |
Verbose ifTrue:[
|
45
|
903 |
Transcript showCr:'openDynamic: ' , pathName
|
20
|
904 |
].
|
|
905 |
|
15
|
906 |
handle := self primOpenDynamicObject:pathName into:(Array new:2).
|
20
|
907 |
handle isNil ifTrue:[
|
45
|
908 |
LastError == #notImplemented ifTrue:[
|
|
909 |
Verbose ifTrue:[
|
|
910 |
Transcript showCr:'no dynamic load facility or load failed.'.
|
|
911 |
].
|
|
912 |
"try it the hard way"
|
|
913 |
handle := self loadFile:pathName.
|
|
914 |
]
|
20
|
915 |
].
|
45
|
916 |
|
|
917 |
"
|
|
918 |
remember loaded object for later unloading
|
|
919 |
"
|
|
920 |
LoadedObjects isNil ifTrue:[
|
|
921 |
LoadedObjects := Dictionary new.
|
|
922 |
].
|
|
923 |
LoadedObjects at:pathName put:handle.
|
|
924 |
|
15
|
925 |
^ handle
|
0
|
926 |
|
|
927 |
"sys5.4:
|
|
928 |
|handle|
|
|
929 |
handle := ObjectFileLoader openDynamicObject:'../stc/mod1.so'.
|
20
|
930 |
ObjectFileLoader getFunction:'module1' from:handle
|
15
|
931 |
"
|
0
|
932 |
"next:
|
|
933 |
|handle|
|
|
934 |
handle := ObjectFileLoader openDynamicObject:'../goodies/Path/AbstrPath.o'.
|
20
|
935 |
ObjectFileLoader getFunction:'__AbstractPath_Init' from:handle
|
15
|
936 |
"
|
|
937 |
"GLD:
|
|
938 |
|handle|
|
|
939 |
handle := ObjectFileLoader openDynamicObject:'../clients/Tetris/Tetris.o'.
|
20
|
940 |
ObjectFileLoader getFunction:'__TetrisBlock_Init' from:handle
|
15
|
941 |
"
|
0
|
942 |
!
|
|
943 |
|
15
|
944 |
primOpenDynamicObject:pathName into:aBuffer
|
6
|
945 |
"open an object-file (map into my address space).
|
28
|
946 |
Return an OS-handle (whatever that is) - where some space
|
|
947 |
(a 2-element array) has to be passed in for this.
|
15
|
948 |
This function is not supported on all architectures.
|
20
|
949 |
Dont depend on the values or types returned in aBuffer,
|
|
950 |
it depends on the underlying dynamic load package."
|
6
|
951 |
|
|
952 |
%{ /* UNLIMITEDSTACK */
|
|
953 |
|
15
|
954 |
#ifdef GNU_DL
|
45
|
955 |
# ifndef dld_h
|
15
|
956 |
# include "dld.h"
|
45
|
957 |
# define dld_h
|
|
958 |
# endif
|
20
|
959 |
static firstCall = 1;
|
|
960 |
extern char *__myName__;
|
|
961 |
|
|
962 |
if (firstCall) {
|
45
|
963 |
firstCall = 0;
|
|
964 |
(void) dld_init (__myName__);
|
20
|
965 |
}
|
|
966 |
|
15
|
967 |
if (__isString(pathName)) {
|
45
|
968 |
if (dld_link(_stringVal(pathName))) {
|
|
969 |
dld_perror("cant link");
|
|
970 |
ObjectFileLoader_LastError = @symbol(linkError);
|
|
971 |
RETURN ( nil );
|
|
972 |
}
|
|
973 |
RETURN ( pathName );
|
15
|
974 |
}
|
|
975 |
RETURN ( nil );
|
|
976 |
#endif
|
|
977 |
|
|
978 |
#ifdef SYSV4_DL
|
28
|
979 |
# ifndef dlfcn_h
|
|
980 |
# include <dlfcn.h>
|
|
981 |
# define dlfcn_h
|
|
982 |
# endif
|
15
|
983 |
void *handle;
|
|
984 |
|
|
985 |
if ((pathName == nil) || __isString(pathName)) {
|
45
|
986 |
if (__isArray(aBuffer)
|
|
987 |
&& (_arraySize(aBuffer) == 2)) {;
|
|
988 |
if (pathName == nil)
|
|
989 |
handle = dlopen((char *)0, RTLD_NOW);
|
|
990 |
else
|
|
991 |
handle = dlopen(_stringVal(pathName), RTLD_NOW);
|
15
|
992 |
|
45
|
993 |
if (! handle) {
|
|
994 |
fprintf(stderr, "dlopen %s error: <%s>\n",
|
|
995 |
_stringVal(pathName), dlerror());
|
|
996 |
ObjectFileLoader_LastError = @symbol(linkError);
|
|
997 |
RETURN (nil);
|
|
998 |
}
|
15
|
999 |
|
45
|
1000 |
if (ObjectFileLoader_Verbose == true)
|
|
1001 |
printf("open %s handle = %x\n", _stringVal(pathName), handle);
|
28
|
1002 |
|
45
|
1003 |
_ArrayInstPtr(aBuffer)->a_element[0] =
|
|
1004 |
_MKSMALLINT( (int)handle & 0xFFFF );
|
|
1005 |
_ArrayInstPtr(aBuffer)->a_element[1] =
|
|
1006 |
_MKSMALLINT( ((int)handle >> 16) & 0xFFFF );
|
|
1007 |
RETURN (aBuffer);
|
|
1008 |
}
|
15
|
1009 |
}
|
|
1010 |
#endif
|
|
1011 |
|
|
1012 |
#ifdef SUN_DL
|
45
|
1013 |
# ifndef dlfcn_h
|
|
1014 |
# include <dlfcn.h>
|
|
1015 |
# define dlfcn_h
|
|
1016 |
# endif
|
6
|
1017 |
void *handle;
|
|
1018 |
|
15
|
1019 |
if ((pathName == nil) || __isString(pathName)) {
|
45
|
1020 |
if (__isArray(aBuffer)
|
|
1021 |
&& (_arraySize(aBuffer) == 2)) {;
|
|
1022 |
if (pathName == nil)
|
|
1023 |
handle = dlopen((char *)0, 1);
|
|
1024 |
else
|
|
1025 |
handle = dlopen(_stringVal(pathName), 1);
|
15
|
1026 |
|
45
|
1027 |
if (! handle) {
|
|
1028 |
fprintf(stderr, "dlopen %s error: <%s>\n",
|
|
1029 |
_stringVal(pathName), dlerror());
|
|
1030 |
ObjectFileLoader_LastError = @symbol(linkError);
|
|
1031 |
RETURN (nil);
|
|
1032 |
}
|
15
|
1033 |
|
45
|
1034 |
if (ObjectFileLoader_Verbose == true)
|
|
1035 |
printf("open %s handle = %x\n", _stringVal(pathName), handle);
|
28
|
1036 |
|
45
|
1037 |
_ArrayInstPtr(aBuffer)->a_element[0] =
|
|
1038 |
_MKSMALLINT( (int)handle & 0xFFFF );
|
|
1039 |
_ArrayInstPtr(aBuffer)->a_element[1] =
|
|
1040 |
_MKSMALLINT( ((int)handle >> 16) & 0xFFFF );
|
|
1041 |
RETURN (aBuffer);
|
|
1042 |
}
|
6
|
1043 |
}
|
|
1044 |
#endif
|
15
|
1045 |
|
|
1046 |
#ifdef NEXT_DL
|
6
|
1047 |
long result;
|
|
1048 |
char *files[2];
|
|
1049 |
NXStream *errOut;
|
|
1050 |
|
15
|
1051 |
if (__isString(pathName)) {
|
45
|
1052 |
files[0] = (char *) _stringVal(pathName);
|
|
1053 |
files[1] = (char *) 0;
|
|
1054 |
errOut = NXOpenFile(2, 2);
|
|
1055 |
result = rld_load(errOut,
|
|
1056 |
(struct mach_header **)0,
|
|
1057 |
files,
|
|
1058 |
(char *)0);
|
|
1059 |
NXClose(errOut);
|
|
1060 |
if (! result) {
|
|
1061 |
ObjectFileLoader_LastError = @symbol(linkError);
|
|
1062 |
fprintf(stderr, "rld_load %s failed\n", _stringVal(pathName));
|
|
1063 |
RETURN (nil);
|
|
1064 |
}
|
15
|
1065 |
|
45
|
1066 |
if (ObjectFileLoader_Verbose == true)
|
|
1067 |
printf("rld_load %s ok\n", _stringVal(pathName));
|
28
|
1068 |
|
45
|
1069 |
RETURN (pathName);
|
6
|
1070 |
}
|
|
1071 |
#endif
|
15
|
1072 |
%}.
|
28
|
1073 |
LastError := #notImplemented.
|
|
1074 |
^ nil
|
6
|
1075 |
!
|
|
1076 |
|
0
|
1077 |
closeDynamicObject:handle
|
45
|
1078 |
"close an object-file (unmap from my address space)
|
|
1079 |
and remove the entry from the remembered object file set."
|
|
1080 |
|
|
1081 |
|key|
|
|
1082 |
|
|
1083 |
(self primCloseDynamicObject:handle) ifTrue:[
|
|
1084 |
"
|
|
1085 |
remove from loaded objects
|
|
1086 |
"
|
|
1087 |
LoadedObjects notNil ifTrue:[
|
|
1088 |
key := LoadedObjects keyAtValue:handle.
|
|
1089 |
key notNil ifTrue:[
|
|
1090 |
LoadedObjects removeKey:key
|
|
1091 |
]
|
|
1092 |
]
|
|
1093 |
] ifFalse:[
|
|
1094 |
self error:'closeDynamic failed'
|
|
1095 |
]
|
|
1096 |
!
|
|
1097 |
|
|
1098 |
primCloseDynamicObject:handle
|
0
|
1099 |
"close an object-file (unmap from my address space)."
|
|
1100 |
|
|
1101 |
|low hi|
|
45
|
1102 |
|
15
|
1103 |
%{
|
|
1104 |
#ifdef GNU_DL
|
45
|
1105 |
# ifndef dld_h
|
15
|
1106 |
# include "dld.h"
|
45
|
1107 |
# define dld_h
|
|
1108 |
# endif
|
15
|
1109 |
if (__isString(handle)) {
|
45
|
1110 |
if (dld_unlink_by_file(_stringVal(handle), 1)) {
|
|
1111 |
dld_perror("cant unlink");
|
|
1112 |
RETURN (false);
|
|
1113 |
}
|
|
1114 |
RETURN (true);
|
15
|
1115 |
}
|
45
|
1116 |
RETURN (false);
|
15
|
1117 |
#endif
|
|
1118 |
%}.
|
28
|
1119 |
low := handle at:1.
|
|
1120 |
hi := handle at:2.
|
0
|
1121 |
%{
|
15
|
1122 |
#ifdef SYSV4_DL
|
28
|
1123 |
# ifndef dlfcn_h
|
|
1124 |
# include <dlfcn.h>
|
|
1125 |
# define dlfcn_h
|
|
1126 |
# endif
|
0
|
1127 |
void *h;
|
|
1128 |
int val;
|
|
1129 |
|
|
1130 |
if (_isSmallInteger(low) && _isSmallInteger(hi)) {
|
45
|
1131 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
1132 |
h = (void *)(val);
|
|
1133 |
if (ObjectFileLoader_Verbose == true)
|
|
1134 |
printf("close handle = %x\n", h);
|
|
1135 |
if (dlclose(h) != 0) {
|
|
1136 |
fprintf(stderr, "dlclose failed with:<%s>\n", dlerror());
|
|
1137 |
RETURN (false);
|
|
1138 |
}
|
|
1139 |
RETURN (true);
|
0
|
1140 |
}
|
|
1141 |
#endif
|
15
|
1142 |
|
|
1143 |
#ifdef SUN_DL
|
45
|
1144 |
# ifndef dlfcn_h
|
|
1145 |
# include <dlfcn.h>
|
|
1146 |
# define dlfcn_h
|
|
1147 |
# endif
|
3
|
1148 |
void *h;
|
|
1149 |
int val;
|
|
1150 |
|
|
1151 |
if (_isSmallInteger(low) && _isSmallInteger(hi)) {
|
45
|
1152 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
1153 |
h = (void *)(val);
|
|
1154 |
if (ObjectFileLoader_Verbose == true)
|
|
1155 |
printf("close handle = %x\n", h);
|
|
1156 |
dlclose(h);
|
|
1157 |
RETURN (true);
|
3
|
1158 |
}
|
|
1159 |
#endif
|
45
|
1160 |
%}.
|
|
1161 |
^ false
|
0
|
1162 |
!
|
|
1163 |
|
20
|
1164 |
isSmalltalkObject:handle
|
|
1165 |
"return true, if the loaded object is a smalltalk object module"
|
|
1166 |
|
|
1167 |
"not yet implemented - stc_compiled_smalltalk is a static symbol,
|
|
1168 |
not found in list - need nm-interface, or nlist-walker
|
|
1169 |
"
|
|
1170 |
^ true.
|
|
1171 |
|
|
1172 |
(self getSymbol:'__stc_compiled_smalltalk' function:true from:handle) notNil ifTrue:[^ true].
|
|
1173 |
(self getSymbol:'__stc_compiled_smalltalk' function:false from:handle) notNil ifTrue:[^ true].
|
|
1174 |
^ false
|
|
1175 |
!
|
|
1176 |
|
|
1177 |
isCPlusPlusObject:handle
|
|
1178 |
"return true, if the loaded object is a c++ object module"
|
|
1179 |
|
|
1180 |
(self getSymbol:'__gnu_compiled_cplusplus' function:true from:handle) notNil ifTrue:[^ true].
|
|
1181 |
(self getSymbol:'__CTOR_LIST__' function:true from:handle) notNil ifTrue:[^ true].
|
|
1182 |
(self getSymbol:'__CTOR_LIST__' function:false from:handle) notNil ifTrue:[^ true].
|
|
1183 |
(self getSymbol:'__gnu_compiled_cplusplus' function:false from:handle) notNil ifTrue:[^ true].
|
|
1184 |
^ false
|
|
1185 |
!
|
|
1186 |
|
28
|
1187 |
namesMatching:aPattern segment:segmentPattern in:aFileName
|
20
|
1188 |
|p l s addr segment name entry|
|
|
1189 |
|
|
1190 |
l := OrderedCollection new.
|
28
|
1191 |
p := PipeStream readingFrom:(self nm:aFileName).
|
20
|
1192 |
p isNil ifTrue:[
|
45
|
1193 |
('cannot read names from ' , aFileName) errorPrintNL.
|
|
1194 |
^ nil
|
20
|
1195 |
].
|
|
1196 |
[p atEnd] whileFalse:[
|
45
|
1197 |
entry := p nextLine.
|
|
1198 |
Verbose ifTrue:[
|
|
1199 |
entry printNL.
|
|
1200 |
].
|
|
1201 |
entry notNil ifTrue:[
|
|
1202 |
s := ReadStream on:entry.
|
|
1203 |
addr := s nextWord.
|
|
1204 |
segment := s nextWord.
|
|
1205 |
name := s upToEnd withoutSeparators.
|
|
1206 |
(addr notNil and:[segment notNil and:[name notNil]]) ifTrue:[
|
|
1207 |
(aPattern match:name) ifTrue:[
|
|
1208 |
(segmentPattern isNil or:[segmentPattern match:segment]) ifTrue:[
|
|
1209 |
l add:name.
|
|
1210 |
Verbose ifTrue:[
|
|
1211 |
'found name: ' print. name printNL.
|
|
1212 |
]
|
|
1213 |
] ifFalse:[
|
|
1214 |
Verbose ifTrue:[
|
|
1215 |
name print. ' segment mismatch ' print.
|
|
1216 |
segmentPattern print. ' ' print. segment printNL.
|
|
1217 |
]
|
|
1218 |
]
|
|
1219 |
]
|
|
1220 |
]
|
|
1221 |
]
|
20
|
1222 |
].
|
|
1223 |
p close.
|
|
1224 |
^ l
|
|
1225 |
!
|
|
1226 |
|
|
1227 |
isObjectiveCObject:handle
|
|
1228 |
"not yet implemented"
|
|
1229 |
|
|
1230 |
^ false
|
|
1231 |
!
|
|
1232 |
|
|
1233 |
getFunction:aString from:handle
|
|
1234 |
"return the address of a function from a dynamically loaded object file.
|
|
1235 |
Handle must be the one returned previously from openDynamicObject.
|
|
1236 |
Return the address of the function, or nil on any error."
|
|
1237 |
|
|
1238 |
^ self getSymbol:aString function:true from:handle
|
|
1239 |
!
|
|
1240 |
|
|
1241 |
getSymbol:aString function:isFunction from:handle
|
|
1242 |
"return the address of a symbol/function from a dynamically loaded object file.
|
0
|
1243 |
Handle must be the one returned previously from openDynamicObject.
|
|
1244 |
Return the address of the symbol, or nil on any error."
|
|
1245 |
|
|
1246 |
|low hi lowAddr hiAddr|
|
|
1247 |
|
20
|
1248 |
%{ /* STACK: 20000 */
|
|
1249 |
|
15
|
1250 |
#ifdef GNU_DL
|
45
|
1251 |
# ifndef dld_h
|
15
|
1252 |
# include "dld.h"
|
45
|
1253 |
# define dld_h
|
|
1254 |
# endif
|
|
1255 |
|
15
|
1256 |
void (*func)();
|
20
|
1257 |
unsigned long addr;
|
|
1258 |
char *name;
|
15
|
1259 |
|
|
1260 |
if (__isString(aString)) {
|
45
|
1261 |
name = (char *) _stringVal(aString);
|
|
1262 |
if (isFunction == false) {
|
|
1263 |
addr = dld_get_symbol(name);
|
|
1264 |
} else {
|
|
1265 |
func = (void (*) ()) dld_get_func(name);
|
|
1266 |
if (func) {
|
|
1267 |
if (ObjectFileLoader_Verbose == true)
|
|
1268 |
printf("addr of %s = %x\n", name, (INT)func);
|
|
1269 |
if (dld_function_executable_p(name)) {
|
|
1270 |
lowAddr = _MKSMALLINT( (INT)func & 0xFFFF );
|
|
1271 |
hiAddr = _MKSMALLINT( ((INT)func >> 16) & 0xFFFF );
|
|
1272 |
} else {
|
|
1273 |
char **undefNames;
|
|
1274 |
char **nm;
|
|
1275 |
int i;
|
20
|
1276 |
|
45
|
1277 |
if (ObjectFileLoader_Verbose == true) {
|
|
1278 |
printf ("function %s not executable\n", name);
|
|
1279 |
dld_perror("not executable");
|
20
|
1280 |
|
45
|
1281 |
printf("undefined:\n");
|
|
1282 |
nm = undefNames = dld_list_undefined_sym();
|
|
1283 |
for (i=dld_undefined_sym_count; i; i--) {
|
|
1284 |
printf(" %s\n", *nm++);
|
|
1285 |
}
|
|
1286 |
}
|
|
1287 |
free(undefNames);
|
|
1288 |
}
|
|
1289 |
} else {
|
|
1290 |
dld_perror("get_func");
|
|
1291 |
}
|
|
1292 |
}
|
15
|
1293 |
}
|
|
1294 |
#endif
|
|
1295 |
%}.
|
|
1296 |
|
28
|
1297 |
low := handle at:1.
|
|
1298 |
hi := handle at:2.
|
15
|
1299 |
%{
|
|
1300 |
#ifdef SYSV4_DL
|
28
|
1301 |
# ifndef dlfcn_h
|
|
1302 |
# include <dlfcn.h>
|
|
1303 |
# define dlfcn_h
|
|
1304 |
# endif
|
0
|
1305 |
void *h;
|
|
1306 |
void *addr;
|
|
1307 |
int val;
|
|
1308 |
|
|
1309 |
if (_isSmallInteger(low) && _isSmallInteger(hi)) {
|
45
|
1310 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
1311 |
h = (void *)(val);
|
|
1312 |
if (__isString(aString)) {
|
|
1313 |
if (ObjectFileLoader_Verbose == true)
|
|
1314 |
printf("get sym <%s> handle = %x\n", _stringVal(aString), h);
|
|
1315 |
addr = dlsym(h, (char *) _stringVal(aString));
|
|
1316 |
if (addr) {
|
|
1317 |
if (ObjectFileLoader_Verbose == true)
|
|
1318 |
printf("addr = %x\n", addr);
|
|
1319 |
lowAddr = _MKSMALLINT( (int)addr & 0xFFFF );
|
|
1320 |
hiAddr = _MKSMALLINT( ((int)addr >> 16) & 0xFFFF );
|
|
1321 |
} else {
|
|
1322 |
if (ObjectFileLoader_Verbose == true)
|
|
1323 |
printf("dlsym %s error: <%s>\n", _stringVal(aString), dlerror());
|
|
1324 |
}
|
|
1325 |
}
|
0
|
1326 |
}
|
|
1327 |
#endif
|
15
|
1328 |
|
|
1329 |
#ifdef SUN_DL
|
3
|
1330 |
# include <dlfcn.h>
|
|
1331 |
void *h;
|
|
1332 |
void *addr;
|
|
1333 |
int val;
|
|
1334 |
|
|
1335 |
if (_isSmallInteger(low) && _isSmallInteger(hi)) {
|
45
|
1336 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
1337 |
h = (void *)(val);
|
|
1338 |
if (__isString(aString)) {
|
|
1339 |
if (ObjectFileLoader_Verbose == true)
|
|
1340 |
printf("get sym <%s> handle = %x\n", _stringVal(aString), h);
|
|
1341 |
addr = dlsym(h, _stringVal(aString));
|
|
1342 |
if (addr) {
|
|
1343 |
if (ObjectFileLoader_Verbose == true)
|
|
1344 |
printf("addr = %x\n", addr);
|
|
1345 |
lowAddr = _MKSMALLINT( (int)addr & 0xFFFF );
|
|
1346 |
hiAddr = _MKSMALLINT( ((int)addr >> 16) & 0xFFFF );
|
|
1347 |
} else {
|
|
1348 |
if (ObjectFileLoader_Verbose == true)
|
|
1349 |
printf("dlsym %s error: <%s>\n", _stringVal(aString), dlerror());
|
|
1350 |
}
|
|
1351 |
}
|
3
|
1352 |
}
|
|
1353 |
#endif
|
15
|
1354 |
|
|
1355 |
#ifdef NEXT_DL
|
0
|
1356 |
unsigned long addr;
|
|
1357 |
long result;
|
|
1358 |
NXStream *errOut;
|
|
1359 |
|
15
|
1360 |
if (__isString(aString)) {
|
45
|
1361 |
if (ObjectFileLoader_Verbose == true)
|
|
1362 |
printf("get sym <%s>\n", _stringVal(aString));
|
|
1363 |
errOut = NXOpenFile(2, 2);
|
|
1364 |
result = rld_lookup(errOut,
|
|
1365 |
(char *) _stringVal(aString),
|
|
1366 |
&addr);
|
|
1367 |
NXClose(errOut);
|
|
1368 |
if (result) {
|
|
1369 |
if (ObjectFileLoader_Verbose == true)
|
|
1370 |
printf("addr = %x\n", addr);
|
|
1371 |
lowAddr = _MKSMALLINT( (int)addr & 0xFFFF );
|
|
1372 |
hiAddr = _MKSMALLINT( ((int)addr >> 16) & 0xFFFF );
|
|
1373 |
}
|
0
|
1374 |
}
|
|
1375 |
#endif
|
|
1376 |
%}
|
|
1377 |
.
|
|
1378 |
lowAddr notNil ifTrue:[
|
45
|
1379 |
^ (hiAddr * 16r10000) + lowAddr
|
0
|
1380 |
].
|
|
1381 |
^ nil
|
|
1382 |
!
|
|
1383 |
|
20
|
1384 |
getListOfUndefinedSymbolsFrom:handle
|
|
1385 |
"return a collection of undefined symbols in a dynamically loaded object file.
|
|
1386 |
Handle must be the one returned previously from openDynamicObject."
|
|
1387 |
|
|
1388 |
|list|
|
|
1389 |
|
|
1390 |
list := Array new:100. "no more than 100 symbols"
|
|
1391 |
%{
|
|
1392 |
|
|
1393 |
#ifdef GNU_DL
|
45
|
1394 |
# ifndef dld_h
|
20
|
1395 |
# include "dld.h"
|
45
|
1396 |
# define dld_h
|
|
1397 |
# endif
|
20
|
1398 |
void (*func)();
|
|
1399 |
unsigned long addr;
|
|
1400 |
char *name;
|
|
1401 |
int nMax;
|
|
1402 |
|
|
1403 |
if (__isArray(list)) {
|
45
|
1404 |
char **undefNames;
|
|
1405 |
char **nm;
|
|
1406 |
int index;
|
20
|
1407 |
|
45
|
1408 |
nMax = _arraySize(list);
|
20
|
1409 |
|
45
|
1410 |
nm = undefNames = dld_list_undefined_sym();
|
|
1411 |
for (index = 0; index < dld_undefined_sym_count; index++) {
|
|
1412 |
_ArrayInstPtr(list)->a_element[index] = _MKSTRING(*nm++);
|
|
1413 |
if (index == nMax)
|
|
1414 |
break;
|
|
1415 |
}
|
|
1416 |
free(undefNames);
|
20
|
1417 |
}
|
|
1418 |
#endif
|
|
1419 |
|
|
1420 |
#ifdef SYSV4_DL
|
|
1421 |
/*
|
|
1422 |
* dont know how to do it
|
|
1423 |
*/
|
|
1424 |
#endif
|
|
1425 |
|
|
1426 |
#ifdef SUN_DL
|
|
1427 |
/*
|
|
1428 |
* dont know how to do it
|
|
1429 |
*/
|
|
1430 |
#endif
|
|
1431 |
|
|
1432 |
#ifdef NEXT_DL
|
|
1433 |
/*
|
|
1434 |
* dont know how to do it
|
|
1435 |
*/
|
|
1436 |
#endif
|
|
1437 |
%}.
|
|
1438 |
^ list
|
|
1439 |
!
|
|
1440 |
|
0
|
1441 |
releaseSymbolTable
|
|
1442 |
"this is needed on NeXT to forget loaded names. If this wasnt done,
|
15
|
1443 |
the same class could not be loaded in again due to multiple defines.
|
0
|
1444 |
On other architectures, this is not needed and therefore a noop."
|
|
1445 |
|
|
1446 |
%{
|
15
|
1447 |
#ifdef NEXT_DL
|
0
|
1448 |
NXStream *errOut;
|
|
1449 |
|
|
1450 |
errOut = NXOpenFile(2, 2);
|
|
1451 |
rld_unload_all(errOut, (long)0);
|
|
1452 |
rld_load_basefile(errOut, "smalltalk");
|
|
1453 |
NXClose(errOut);
|
|
1454 |
#endif
|
|
1455 |
%}
|
|
1456 |
!
|
|
1457 |
|
20
|
1458 |
callInitFunctionAt:initAddr
|
|
1459 |
"
|
37
|
1460 |
need 4 passes to init: 0: create my pools
|
45
|
1461 |
1: get var-refs to other pools
|
|
1462 |
2: install class, methods and literals
|
|
1463 |
3: send #initialize to class
|
20
|
1464 |
"
|
|
1465 |
self callFunctionAt:initAddr forceOld:true arg:0.
|
|
1466 |
self callFunctionAt:initAddr forceOld:true arg:1.
|
|
1467 |
self callFunctionAt:initAddr forceOld:true arg:2.
|
37
|
1468 |
self callFunctionAt:initAddr forceOld:false arg:3.
|
20
|
1469 |
!
|
|
1470 |
|
|
1471 |
callFunctionAt:address forceOld:forceOld arg:argument
|
0
|
1472 |
"call a function at address - this is very dangerous.
|
|
1473 |
This is needed to call the classes init-function after loading in a
|
37
|
1474 |
class-object file. ForceOld (if true) will have the memory manager
|
|
1475 |
allocate things in oldSpace instead of newSpace.
|
|
1476 |
DANGER: Internal. Dont use in your programs."
|
0
|
1477 |
|
|
1478 |
|low hi lowAddr hiAddr|
|
|
1479 |
|
|
1480 |
hi := address // 16r10000.
|
|
1481 |
low := address \\ 16r10000.
|
|
1482 |
%{
|
|
1483 |
void (*addr)();
|
|
1484 |
unsigned val;
|
|
1485 |
typedef void (*VOIDFUNC)();
|
19
|
1486 |
int savInt;
|
|
1487 |
extern int _immediateInterrupt;
|
20
|
1488 |
int prevSpace;
|
|
1489 |
int arg = 0;
|
0
|
1490 |
|
|
1491 |
if (_isSmallInteger(low) && _isSmallInteger(hi)) {
|
45
|
1492 |
val = (_intVal(hi) << 16) + _intVal(low);
|
|
1493 |
addr = (VOIDFUNC) val;
|
19
|
1494 |
|
45
|
1495 |
if (_isSmallInteger(argument)) {
|
|
1496 |
arg = _intVal(argument);
|
|
1497 |
}
|
|
1498 |
/*
|
|
1499 |
* allow function to be interrupted
|
|
1500 |
*/
|
|
1501 |
savInt = _immediateInterrupt;
|
|
1502 |
_immediateInterrupt = 1;
|
19
|
1503 |
|
45
|
1504 |
if (forceOld == true) {
|
|
1505 |
prevSpace = __allocForceSpace(OLDSPACE);
|
|
1506 |
(*addr)(arg COMMA_CON);
|
|
1507 |
__allocForceSpace(prevSpace);
|
|
1508 |
} else {
|
|
1509 |
(*addr)(arg COMMA_CON);
|
|
1510 |
}
|
19
|
1511 |
|
45
|
1512 |
_immediateInterrupt = savInt;
|
0
|
1513 |
}
|
|
1514 |
%}
|
|
1515 |
! !
|
|
1516 |
|
20
|
1517 |
!ObjectFileLoader class methodsFor:'primitive loading'!
|
|
1518 |
|
|
1519 |
textSizeOf:aFileName
|
|
1520 |
"
|
|
1521 |
get the size of the text-segment (nBytes)
|
|
1522 |
"
|
|
1523 |
|
|
1524 |
%{ /* NOCONTEXT */
|
|
1525 |
#ifdef HAS_DL
|
|
1526 |
/*
|
|
1527 |
* not needed, if dynamic link facilities exist
|
|
1528 |
*/
|
|
1529 |
#else /* no DL-support */
|
|
1530 |
char *fname;
|
|
1531 |
int fd;
|
|
1532 |
|
|
1533 |
if (! __isString(aFileName)) {
|
45
|
1534 |
RETURN (nil);
|
20
|
1535 |
}
|
|
1536 |
|
|
1537 |
fname = (char *) _stringVal(aFileName);
|
|
1538 |
|
|
1539 |
# if defined(A_DOT_OUT) && !defined(ELF)
|
|
1540 |
# if !defined(sco) && !defined(isc)
|
|
1541 |
{
|
45
|
1542 |
struct exec header;
|
20
|
1543 |
|
45
|
1544 |
if ((fd = open(fname, 0)) < 0) {
|
|
1545 |
fprintf(stderr, "cannot open <%s>\n", fname);
|
|
1546 |
RETURN ( nil );
|
|
1547 |
}
|
|
1548 |
if (read(fd, &header, sizeof(header)) != sizeof(header)) {
|
|
1549 |
fprintf(stderr, "cannot read header of <%s>\n", fname);
|
|
1550 |
close(fd);
|
|
1551 |
RETURN ( nil );
|
|
1552 |
}
|
|
1553 |
close(fd);
|
20
|
1554 |
|
45
|
1555 |
if (N_MAGIC(header) != OMAGIC) {
|
|
1556 |
fprintf(stderr, "header is (0%o) %x - should be (0%o)%x\n",
|
|
1557 |
N_MAGIC(header), N_MAGIC(header),
|
|
1558 |
OMAGIC, OMAGIC);
|
|
1559 |
RETURN ( nil );
|
|
1560 |
}
|
|
1561 |
RETURN ( _MKSMALLINT(header.a_text) );
|
20
|
1562 |
}
|
|
1563 |
# endif
|
|
1564 |
# endif
|
|
1565 |
/*
|
|
1566 |
* need support for other headers ... (i.e. coff, elf)
|
|
1567 |
*/
|
|
1568 |
#endif
|
|
1569 |
%}.
|
|
1570 |
^ self error:'objectFile format not supported'
|
|
1571 |
!
|
|
1572 |
|
|
1573 |
dataSizeOf:aFileName
|
|
1574 |
"
|
|
1575 |
get the size of the data-segment (nBytes)
|
|
1576 |
"
|
|
1577 |
|
|
1578 |
%{ /* NOCONTEXT */
|
|
1579 |
#ifdef HAS_DL
|
|
1580 |
/*
|
|
1581 |
* not needed, if dynamic link facilities exist
|
|
1582 |
*/
|
|
1583 |
#else /* no DL-support */
|
|
1584 |
char *fname;
|
|
1585 |
int fd;
|
|
1586 |
|
|
1587 |
if (! __isString(aFileName)) {
|
45
|
1588 |
RETURN ( nil );
|
20
|
1589 |
}
|
|
1590 |
|
|
1591 |
fname = (char *) _stringVal(aFileName);
|
|
1592 |
|
|
1593 |
# if defined(A_DOT_OUT) && !defined(ELF)
|
|
1594 |
# if !defined(sco) && !defined(isc)
|
|
1595 |
{
|
45
|
1596 |
struct exec header;
|
|
1597 |
unsigned size;
|
20
|
1598 |
|
45
|
1599 |
if ((fd = open(fname, 0)) < 0) {
|
|
1600 |
fprintf(stderr, "cannot open <%s>\n", fname);
|
|
1601 |
RETURN ( nil );
|
|
1602 |
}
|
|
1603 |
if (read(fd, &header, sizeof(header)) != sizeof(header)) {
|
|
1604 |
fprintf(stderr, "cannot read header of <%s>\n", fname);
|
|
1605 |
close(fd);
|
|
1606 |
RETURN ( nil );
|
|
1607 |
}
|
|
1608 |
close(fd);
|
20
|
1609 |
|
45
|
1610 |
if (N_MAGIC(header) != OMAGIC) {
|
|
1611 |
fprintf(stderr, "header is (0%o) %x - should be (0%o)%x\n",
|
|
1612 |
N_MAGIC(header), N_MAGIC(header),
|
|
1613 |
OMAGIC, OMAGIC);
|
|
1614 |
RETURN ( nil );
|
|
1615 |
}
|
|
1616 |
size = header.a_data;
|
20
|
1617 |
# if defined(sinix) && defined(BSD)
|
45
|
1618 |
size += header.a_bss;
|
20
|
1619 |
# endif
|
45
|
1620 |
RETURN ( _MKSMALLINT(size) );
|
20
|
1621 |
}
|
|
1622 |
# endif
|
|
1623 |
# endif
|
|
1624 |
/*
|
|
1625 |
* need support for other headers ... (i.e. coff, elf)
|
|
1626 |
*/
|
|
1627 |
#endif
|
|
1628 |
%}
|
|
1629 |
.
|
|
1630 |
^ self error:'objectFile format not supported'
|
|
1631 |
!
|
|
1632 |
|
|
1633 |
loadObjectFile:aFileName textAddr:textAddr textSize:textSize
|
45
|
1634 |
dataAddr:dataAddr dataSize:dataSize
|
20
|
1635 |
|
|
1636 |
"the object in aFileName must have been linked for
|
|
1637 |
absolute address textAddr/dataAddr (using ld -A).
|
|
1638 |
Load the contents from the file. Memory must have previously
|
|
1639 |
been allocated using ExternalBytes."
|
|
1640 |
|
|
1641 |
%{ /* NOCONTEXT */
|
|
1642 |
#ifdef HAS_DL
|
|
1643 |
/*
|
|
1644 |
* not needed, if dynamic link facilities exist
|
|
1645 |
*/
|
|
1646 |
#else /* no DL-support */
|
|
1647 |
if (! __isString(aFileName)) {
|
45
|
1648 |
RETURN ( nil );
|
20
|
1649 |
}
|
|
1650 |
|
|
1651 |
# if defined(A_DOT_OUT) && !defined(ELF)
|
|
1652 |
# if !defined(sco) && !defined(isc)
|
|
1653 |
{
|
45
|
1654 |
char *fname = (char *) _stringVal(aFileName);
|
|
1655 |
unsigned taddr, daddr;
|
|
1656 |
unsigned tsize, dsize;
|
|
1657 |
unsigned toffset = 0;
|
|
1658 |
unsigned doffset = 0;
|
|
1659 |
int fd;
|
|
1660 |
struct exec header;
|
|
1661 |
char *cp;
|
|
1662 |
int bssCount;
|
|
1663 |
unsigned magic = OMAGIC;
|
|
1664 |
int nread;
|
20
|
1665 |
|
45
|
1666 |
taddr = _isSmallInteger(textAddr) ? (unsigned) _intVal(textAddr) : 0;
|
|
1667 |
daddr = _isSmallInteger(dataAddr) ? (unsigned) _intVal(dataAddr) : 0;
|
|
1668 |
tsize = _isSmallInteger(textSize) ? _intVal(textSize) : 0;
|
|
1669 |
dsize = _isSmallInteger(dataSize) ? _intVal(dataSize) : 0;
|
20
|
1670 |
|
45
|
1671 |
if ((fd = open(fname, 0)) < 0) {
|
|
1672 |
fprintf(stderr, "cannot open <%s>\n", fname);
|
|
1673 |
RETURN ( nil );
|
|
1674 |
}
|
|
1675 |
if (read(fd, &header, sizeof(header)) != sizeof(header)) {
|
|
1676 |
fprintf(stderr, "cannot read header of <%s>\n", fname);
|
|
1677 |
close(fd);
|
|
1678 |
RETURN ( nil );
|
|
1679 |
}
|
|
1680 |
if (N_MAGIC(header) != magic) {
|
|
1681 |
fprintf(stderr, "header is (0%o) %x should be (0%o) %x\n",
|
|
1682 |
N_MAGIC(header), N_MAGIC(header),
|
|
1683 |
magic, magic);
|
|
1684 |
close(fd);
|
|
1685 |
RETURN ( nil );
|
|
1686 |
}
|
20
|
1687 |
|
45
|
1688 |
/*
|
|
1689 |
* some linkers produce a huge output file, with zeros up to the
|
|
1690 |
* real code ... - thats what toffset, doffset are for.
|
|
1691 |
*/
|
20
|
1692 |
# if defined(sinix) && defined(BSD)
|
45
|
1693 |
toffset = N_TXTADDR(header);
|
|
1694 |
doffset = toffset + taddr + tsize /* - 0x800 */;
|
|
1695 |
daddr = taddr + tsize;
|
20
|
1696 |
# else
|
|
1697 |
# if defined(mips) && defined(ultrix)
|
45
|
1698 |
toffset = N_TXTOFF(header.ex_f, header.ex_o);
|
|
1699 |
doffset = toffset + tsize;
|
|
1700 |
daddr = taddr + tsize;
|
20
|
1701 |
# else
|
|
1702 |
# if defined(N_TXTOFF)
|
45
|
1703 |
toffset = N_TXTOFF(header);
|
|
1704 |
doffset = N_DATOFF(header);
|
|
1705 |
daddr = taddr + tsize;
|
20
|
1706 |
# else
|
45
|
1707 |
fprintf(stderr, "dont know text/data offsets in objectfile\n");
|
|
1708 |
RETURN ( nil );
|
20
|
1709 |
# endif
|
|
1710 |
# endif
|
|
1711 |
# endif
|
|
1712 |
|
|
1713 |
# ifdef SUPERDEBUG
|
45
|
1714 |
printf("toffs:%x taddr:%x tsize:%d doffs:%x daddr:%x dsize:%d\n",
|
|
1715 |
toffset, taddr, tsize, doffset,daddr, dsize);
|
20
|
1716 |
# endif
|
|
1717 |
|
45
|
1718 |
if (lseek(fd, (long)toffset, 0) < 0) {
|
|
1719 |
fprintf(stderr, "cannot seek to text\n");
|
|
1720 |
close(fd);
|
|
1721 |
RETURN ( nil );
|
|
1722 |
}
|
|
1723 |
if ((nread = read(fd, taddr, tsize)) != tsize) {
|
|
1724 |
perror("read");
|
|
1725 |
fprintf(stderr, "cannot read text wanted:%d got:%d\n", tsize, nread);
|
|
1726 |
close(fd);
|
|
1727 |
RETURN ( nil );
|
|
1728 |
}
|
20
|
1729 |
|
|
1730 |
# ifdef SUPERDEBUG
|
45
|
1731 |
printf("1st bytes of text: %02x %02x %02x %02x\n",
|
|
1732 |
*((char *)taddr) & 0xFF, *((char *)taddr+1) & 0xFF,
|
|
1733 |
*((char *)taddr+2) & 0xFF, *((char *)taddr+3) & 0xFF);
|
20
|
1734 |
# endif
|
|
1735 |
|
45
|
1736 |
if (dsize) {
|
|
1737 |
if (lseek(fd, (long)doffset, 0) < 0) {
|
|
1738 |
fprintf(stderr, "cannot seek to data\n");
|
|
1739 |
close(fd);
|
|
1740 |
RETURN ( nil );
|
|
1741 |
}
|
20
|
1742 |
|
45
|
1743 |
if (read(fd, daddr, dsize) != dsize) {
|
|
1744 |
fprintf(stderr, "cannot read data\n");
|
|
1745 |
close(fd);
|
|
1746 |
RETURN ( nil );
|
|
1747 |
}
|
20
|
1748 |
# ifdef SUPERDEBUG
|
45
|
1749 |
{
|
|
1750 |
char *ptr;
|
|
1751 |
int i;
|
20
|
1752 |
|
45
|
1753 |
ptr = (char *)daddr;
|
|
1754 |
fprintf(stderr, "bytes of data (at %x):\n", ptr);
|
|
1755 |
for (i=dsize; i>0; i--, ptr++)
|
|
1756 |
printf("%02x ", *ptr & 0xFF);
|
|
1757 |
}
|
20
|
1758 |
# endif
|
45
|
1759 |
}
|
|
1760 |
close(fd);
|
20
|
1761 |
|
|
1762 |
# ifdef NOTDEF
|
45
|
1763 |
if (header.a_bss != 0) {
|
|
1764 |
fprintf(stderr, "warning: bss not empty\n");
|
|
1765 |
cp = ((char *)daddr) + header.a_data;
|
|
1766 |
for (bssCount=header.a_bss; bssCount; bssCount--)
|
|
1767 |
*cp++ = 0;
|
|
1768 |
}
|
20
|
1769 |
# endif
|
|
1770 |
}
|
|
1771 |
RETURN ( self );
|
|
1772 |
# endif
|
|
1773 |
# endif
|
|
1774 |
/*
|
|
1775 |
* need support for other headers ... (i.e. coff, elf)
|
|
1776 |
*/
|
|
1777 |
#endif
|
|
1778 |
%}.
|
|
1779 |
^ self error:'objectFile format not supported'
|
|
1780 |
! !
|
|
1781 |
|
0
|
1782 |
ObjectFileLoader initialize!
|