2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* test of standalone rld
25 * usage: rldtest <sarld> <kernel> <driver>
30 #include <streams/streams.h>
31 #include <mach-o/loader.h>
35 typedef unsigned int entry_t
;
36 struct mach_header head
;
38 #define zalloc(a) malloc(a)
39 #define zfree(a) free(a)
42 #define min(a,b) ((a) < (b) ? (a) : (b))
44 extern void *malloc(int size
);
46 struct mach_header
*head
,
57 struct xxx_thread_command
{
59 unsigned long cmdsize
;
62 i386_thread_state_t state
;
66 unsigned int vmaddr
= ~0;
68 // XXX should check cputype
69 cmds
= (unsigned int) zalloc(head
->sizeofcmds
);
70 b_lseek(io
, sizeof (struct mach_header
) + file_offset
, 0);
72 if (read(io
, cmds
, head
->sizeofcmds
) != head
->sizeofcmds
)
73 {printf("error reading commands\n");
76 for (ncmds
= head
->ncmds
, cp
= cmds
; ncmds
> 0; ncmds
--)
82 #define lcp ((struct load_command *)cp)
87 #define scp ((struct segment_command *)cp)
89 addr
= (scp
->vmaddr
& 0x3fffffff) + (int)*raddr
;
91 // Is this an OK assumption?
92 // if the filesize is zero, it doesn't
93 // take up any virtual space...
94 // (Hopefully this only excludes PAGEZERO.)
95 // Also, ignore linkedit segment when
96 // computing size, because we will erase
97 // the linkedit segment later.
98 if(strncmp(scp
->segname
, SEG_LINKEDIT
,
99 sizeof(scp
->segname
)) != 0)
100 vmsize
+= scp
->vmsize
;
101 vmaddr
= min(vmaddr
, addr
);
103 // Zero any space at the end of the segment.
104 bzero((char *)(addr
+ scp
->filesize
),
105 scp
->vmsize
- scp
->filesize
);
107 // FIXME: check to see if we overflow
108 // the available space (should be passed in
109 // as the size argument).
111 b_lseek(io
, scp
->fileoff
+ file_offset
, 0);
112 if (xread(io
, (char *)addr
, scp
->filesize
)
114 printf("Error loading section\n");
115 printf("File size =%x; fileoff_set=%x; addr=%x\n",
116 scp
->filesize
, scp
->fileoff
, addr
);
124 th
= (struct xxx_thread_command
*)cp
;
125 entry
= th
->state
.eip
;
132 // kernBootStruct->rootdev = (dev & 0xffffff00) | devMajor[Dev(dev)];
135 *rentry
= (entry_t
)( (int) entry
& 0x3fffffff );
137 *raddr
= (char *)vmaddr
;
142 printf("Read error\n");
149 entry_t
*entry
, // entry point
150 char **addr
, // load address
151 int *size
// size of loaded program
154 /* get file header */
155 read(fd
, &head
, sizeof(head
));
157 if (head
.magic
== MH_MAGIC
) {
158 return loadmacho((struct mach_header
*) &head
,
159 dev
, fd
, entry
, addr
, size
,0);
162 else if (head
.magic
== 0xbebafeca)
164 printf("no fat binary support yet\n");
168 printf("unrecognized binary format\n");
172 #define DRIVER "/usr/Devices/EtherExpress16.config/EtherExpress16_reloc"
176 fprintf(stderr
,"usage: rldtest <sarld> <kernel> <driver>\n");
180 main(int argc
, char **argv
)
183 char *mem
, *workmem
, *ebuf
;
184 char *laddr
, *kaddr
, *daddr
;
186 struct mach_header
*mh
;
187 int ret
, size
, ksize
, dsize
, count
;
191 char *kernel
, *sarld
, *driver
;
198 mem
= malloc(1024 * 1024 * 16);
199 printf("mem = %x\n",mem
);
203 fprintf(stderr
, "couldn't open sarld %s, error %d\n",sarld
,errno
);
206 printf("fd = %d\n",fd
);
207 loadprog(0, fd
, &entry
, &laddr
, &size
);
209 printf("entry = %x, laddr = %x, size = %d\n",entry
, laddr
, size
);
210 fn
= (int (*)())entry
;
213 fprintf(stderr
, "couldn't open kernel %s, error %d\n",kernel
,errno
);
217 loadprog(0, fd
, &entry
, &kaddr
, &ksize
);
218 printf("entry = %x, kaddr = %x, ksize = %d\n",entry
, kaddr
, ksize
);
220 daddr
= (char *)0x70000;
223 fprintf(stderr
, "couldn't open driver %s, error %d\n",driver
,errno
);
228 count
= read(fd
, daddr
, 65536);
232 daddr
= (char *)0x70000;
233 printf("entry = %x, daddr = %x, size = %d\n",entry
, daddr
, size
);
235 workmem
= malloc(300 * 1024);
236 ebuf
= malloc(64 * 1024);
238 dsize
= 16 * 1024 * 1024 - (int)kaddr
- (int)ksize
;
239 printf("about to call %x\n",fn
);
240 ret
= (*fn
)( "mach_kernel", kaddr
,
241 "driver", daddr
, size
,
242 kaddr
+ ksize
, &dsize
,
246 printf("rld return: %d '%s'\n",ret
, ebuf
);
248 #define SEG_OBJC "__OBJC"
249 sp
= getsectbyname ("__TEXT", "__text");
250 printf("text section: %s\n",sp
->sectname
);
251 sp
= getsectbynamefromheader (kaddr
+ksize
, "__OBJC", "__module_info");
252 printf("objc section: %s\n",sp
->sectname
);
253 sp
= getsectdatafromheader (kaddr
+ksize
,
254 SEG_OBJC
, "__module_info", &size
);
255 printf("objc section: %s\n",sp
->sectname
);
257 printf("getsectdata ret = %d\n",sp
);