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