2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* test of standalone rld
26 * usage: rldtest <sarld> <kernel> <driver>
31 #include <streams/streams.h>
32 #include <mach-o/loader.h>
36 typedef unsigned int entry_t
;
37 struct mach_header head
;
39 #define zalloc(a) malloc(a)
40 #define zfree(a) free(a)
43 #define min(a,b) ((a) < (b) ? (a) : (b))
45 extern void *malloc(int size
);
47 struct mach_header
*head
,
58 struct xxx_thread_command
{
60 unsigned long cmdsize
;
63 i386_thread_state_t state
;
67 unsigned int vmaddr
= ~0;
69 // XXX should check cputype
70 cmds
= (unsigned int) zalloc(head
->sizeofcmds
);
71 b_lseek(io
, sizeof (struct mach_header
) + file_offset
, 0);
73 if (read(io
, cmds
, head
->sizeofcmds
) != head
->sizeofcmds
)
74 {printf("error reading commands\n");
77 for (ncmds
= head
->ncmds
, cp
= cmds
; ncmds
> 0; ncmds
--)
83 #define lcp ((struct load_command *)cp)
88 #define scp ((struct segment_command *)cp)
90 addr
= (scp
->vmaddr
& 0x3fffffff) + (int)*raddr
;
92 // Is this an OK assumption?
93 // if the filesize is zero, it doesn't
94 // take up any virtual space...
95 // (Hopefully this only excludes PAGEZERO.)
96 // Also, ignore linkedit segment when
97 // computing size, because we will erase
98 // the linkedit segment later.
99 if(strncmp(scp
->segname
, SEG_LINKEDIT
,
100 sizeof(scp
->segname
)) != 0)
101 vmsize
+= scp
->vmsize
;
102 vmaddr
= min(vmaddr
, addr
);
104 // Zero any space at the end of the segment.
105 bzero((char *)(addr
+ scp
->filesize
),
106 scp
->vmsize
- scp
->filesize
);
108 // FIXME: check to see if we overflow
109 // the available space (should be passed in
110 // as the size argument).
112 b_lseek(io
, scp
->fileoff
+ file_offset
, 0);
113 if (xread(io
, (char *)addr
, scp
->filesize
)
115 printf("Error loading section\n");
116 printf("File size =%x; fileoff_set=%x; addr=%x\n",
117 scp
->filesize
, scp
->fileoff
, addr
);
125 th
= (struct xxx_thread_command
*)cp
;
126 entry
= th
->state
.eip
;
133 // kernBootStruct->rootdev = (dev & 0xffffff00) | devMajor[Dev(dev)];
136 *rentry
= (entry_t
)( (int) entry
& 0x3fffffff );
138 *raddr
= (char *)vmaddr
;
143 printf("Read error\n");
150 entry_t
*entry
, // entry point
151 char **addr
, // load address
152 int *size
// size of loaded program
155 /* get file header */
156 read(fd
, &head
, sizeof(head
));
158 if (head
.magic
== MH_MAGIC
) {
159 return loadmacho((struct mach_header
*) &head
,
160 dev
, fd
, entry
, addr
, size
,0);
163 else if (head
.magic
== 0xbebafeca)
165 printf("no fat binary support yet\n");
169 printf("unrecognized binary format\n");
173 #define DRIVER "/usr/Devices/EtherExpress16.config/EtherExpress16_reloc"
177 fprintf(stderr
,"usage: rldtest <sarld> <kernel> <driver>\n");
181 main(int argc
, char **argv
)
184 char *mem
, *workmem
, *ebuf
;
185 char *laddr
, *kaddr
, *daddr
;
187 struct mach_header
*mh
;
188 int ret
, size
, ksize
, dsize
, count
;
192 char *kernel
, *sarld
, *driver
;
199 mem
= malloc(1024 * 1024 * 16);
200 printf("mem = %x\n",mem
);
204 fprintf(stderr
, "couldn't open sarld %s, error %d\n",sarld
,errno
);
207 printf("fd = %d\n",fd
);
208 loadprog(0, fd
, &entry
, &laddr
, &size
);
210 printf("entry = %x, laddr = %x, size = %d\n",entry
, laddr
, size
);
211 fn
= (int (*)())entry
;
214 fprintf(stderr
, "couldn't open kernel %s, error %d\n",kernel
,errno
);
218 loadprog(0, fd
, &entry
, &kaddr
, &ksize
);
219 printf("entry = %x, kaddr = %x, ksize = %d\n",entry
, kaddr
, ksize
);
221 daddr
= (char *)0x70000;
224 fprintf(stderr
, "couldn't open driver %s, error %d\n",driver
,errno
);
229 count
= read(fd
, daddr
, 65536);
233 daddr
= (char *)0x70000;
234 printf("entry = %x, daddr = %x, size = %d\n",entry
, daddr
, size
);
236 workmem
= malloc(300 * 1024);
237 ebuf
= malloc(64 * 1024);
239 dsize
= 16 * 1024 * 1024 - (int)kaddr
- (int)ksize
;
240 printf("about to call %x\n",fn
);
241 ret
= (*fn
)( "mach_kernel", kaddr
,
242 "driver", daddr
, size
,
243 kaddr
+ ksize
, &dsize
,
247 printf("rld return: %d '%s'\n",ret
, ebuf
);
249 #define SEG_OBJC "__OBJC"
250 sp
= getsectbyname ("__TEXT", "__text");
251 printf("text section: %s\n",sp
->sectname
);
252 sp
= getsectbynamefromheader (kaddr
+ksize
, "__OBJC", "__module_info");
253 printf("objc section: %s\n",sp
->sectname
);
254 sp
= getsectdatafromheader (kaddr
+ksize
,
255 SEG_OBJC
, "__module_info", &size
);
256 printf("objc section: %s\n",sp
->sectname
);
258 printf("getsectdata ret = %d\n",sp
);