]>
git.saurik.com Git - apple/boot.git/blob - gen/util/machOconv.c
c1296a921c58f52ca45814f62f5a695c9c77708c
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>
29 #include <mach-o/loader.h>
30 #include <libkern/OSByteOrder.h>
34 struct mach_header mh
;
39 static unsigned long swap(
44 return OSSwapInt32(x
);
59 infile
= open(argv
[1], O_RDONLY
);
62 outfile
= fileno(stdout
);
65 infile
= open(argv
[1], O_RDONLY
);
68 outfile
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
74 fprintf(stderr
, "usage: machOconv inputfile [outputfile]\n");
78 nc
= read(infile
, &mh
, sizeof (mh
));
80 perror("read mach header");
83 if (nc
< sizeof (mh
)) {
84 fprintf(stderr
, "read mach header: premature EOF %d\n", nc
);
87 if (mh
.magic
== MH_MAGIC
)
89 else if (mh
.magic
== MH_CIGAM
)
92 fprintf(stderr
, "bad magic number %x\n", mh
.magic
);
96 cmds
= calloc(swap(mh
.sizeofcmds
), sizeof (char));
98 fprintf(stderr
, "alloc load commands: no memory\n");
101 nc
= read(infile
, cmds
, swap(mh
.sizeofcmds
));
103 perror("read load commands");
106 if (nc
< swap(mh
.sizeofcmds
)) {
107 fprintf(stderr
, "read load commands: premature EOF %d\n", nc
);
111 for ( ncmds
= swap(mh
.ncmds
), cp
= cmds
;
112 ncmds
> 0; ncmds
--) {
116 #define lcp ((struct load_command *)cp)
117 switch(swap(lcp
->cmd
)) {
120 #define scp ((struct segment_command *)cp)
121 isDATA
= (strcmp(scp
->segname
, "__DATA") == 0);
123 vmsize
= swap(scp
->filesize
);
125 vmsize
= swap(scp
->vmsize
);
126 result
= vm_allocate(mach_task_self(), &data
, vmsize
, TRUE
);
127 if (result
!= KERN_SUCCESS
) {
128 mach_error("vm_allocate segment data", result
);
132 b_lseek(infile
, swap(scp
->fileoff
), L_SET
);
133 nc
= read(infile
, data
, swap(scp
->filesize
));
135 perror("read segment data");
138 if (nc
< swap(scp
->filesize
)) {
139 fprintf(stderr
, "read segment data: premature EOF %d\n", nc
);
143 nc
= write(outfile
, data
, vmsize
);
145 perror("write segment data");
149 vm_deallocate(mach_task_self(), data
, vmsize
);
153 cp
+= swap(lcp
->cmdsize
);