]>
git.saurik.com Git - apple/boot.git/blob - i386/util/machOconv.c
78aac622ffc79166337c590d49e9ab21a9d14dda
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@
26 #include <mach/mach.h>
27 #include <mach/mach_error.h>
29 #include <mach-o/loader.h>
30 #include <architecture/byte_order.h>
35 struct mach_header mh
;
40 static unsigned long swap(
51 main(int argc
, char *argv
[])
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 %lx\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 lseek(infile
, swap(scp
->fileoff
), L_SET
);
133 nc
= read(infile
, (void *)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
, (void *)data
, vmsize
);
145 perror("write segment data");
149 vm_deallocate(mach_task_self(), data
, vmsize
);
153 cp
+= swap(lcp
->cmdsize
);