]>
git.saurik.com Git - apple/boot.git/blob - i386/util/machOconv.c
e69dddb453cf706944c219b324a2c84e67f4312b
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@
27 #include <mach/mach.h>
28 #include <mach/mach_error.h>
30 #include <mach-o/loader.h>
31 #include <libkern/OSByteOrder.h>
36 struct mach_header mh
;
41 static unsigned long swap(
46 return OSSwapInt32(x
);
52 main(int argc
, char *argv
[])
60 infile
= open(argv
[1], O_RDONLY
);
63 outfile
= fileno(stdout
);
66 infile
= open(argv
[1], O_RDONLY
);
69 outfile
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
75 fprintf(stderr
, "usage: machOconv inputfile [outputfile]\n");
79 nc
= read(infile
, &mh
, sizeof (mh
));
81 perror("read mach header");
84 if (nc
< (int)sizeof (mh
)) {
85 fprintf(stderr
, "read mach header: premature EOF %d\n", nc
);
88 if (mh
.magic
== MH_MAGIC
)
90 else if (mh
.magic
== MH_CIGAM
)
93 fprintf(stderr
, "bad magic number %lx\n", mh
.magic
);
97 cmds
= calloc(swap(mh
.sizeofcmds
), sizeof (char));
99 fprintf(stderr
, "alloc load commands: no memory\n");
102 nc
= read(infile
, cmds
, swap(mh
.sizeofcmds
));
104 perror("read load commands");
107 if (nc
< (int)swap(mh
.sizeofcmds
)) {
108 fprintf(stderr
, "read load commands: premature EOF %d\n", nc
);
112 for ( ncmds
= swap(mh
.ncmds
), cp
= cmds
;
113 ncmds
> 0; ncmds
--) {
117 #define lcp ((struct load_command *)cp)
118 switch(swap(lcp
->cmd
)) {
121 #define scp ((struct segment_command *)cp)
122 isDATA
= (strcmp(scp
->segname
, "__DATA") == 0);
124 vmsize
= swap(scp
->filesize
);
126 vmsize
= swap(scp
->vmsize
);
127 result
= vm_allocate(mach_task_self(), &data
, vmsize
, TRUE
);
128 if (result
!= KERN_SUCCESS
) {
129 mach_error("vm_allocate segment data", result
);
133 lseek(infile
, swap(scp
->fileoff
), L_SET
);
134 nc
= read(infile
, (void *)data
, swap(scp
->filesize
));
136 perror("read segment data");
139 if (nc
< (int)swap(scp
->filesize
)) {
140 fprintf(stderr
, "read segment data: premature EOF %d\n", nc
);
144 nc
= write(outfile
, (void *)data
, vmsize
);
145 if (nc
< (int)vmsize
) {
146 perror("write segment data");
150 vm_deallocate(mach_task_self(), data
, vmsize
);
154 cp
+= swap(lcp
->cmdsize
);