]> git.saurik.com Git - apple/boot.git/blob - i386/util/machOconv.c
e69dddb453cf706944c219b324a2c84e67f4312b
[apple/boot.git] / i386 / util / machOconv.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <mach/mach.h>
28 #include <mach/mach_error.h>
29 #include <sys/file.h>
30 #include <mach-o/loader.h>
31 #include <libkern/OSByteOrder.h>
32 #include <unistd.h>
33
34 int infile, outfile;
35
36 struct mach_header mh;
37 void * cmds;
38
39 boolean_t swap_ends;
40
41 static unsigned long swap(
42 unsigned long x
43 )
44 {
45 if (swap_ends)
46 return OSSwapInt32(x);
47 else
48 return x;
49 }
50
51 int
52 main(int argc, char *argv[])
53 {
54 kern_return_t result;
55 vm_address_t data;
56 int nc, ncmds;
57 char * cp;
58
59 if (argc == 2) {
60 infile = open(argv[1], O_RDONLY);
61 if (infile < 0)
62 goto usage;
63 outfile = fileno(stdout);
64 }
65 else if (argc == 3) {
66 infile = open(argv[1], O_RDONLY);
67 if (infile < 0)
68 goto usage;
69 outfile = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644);
70 if (outfile < 0)
71 goto usage;
72 }
73 else {
74 usage:
75 fprintf(stderr, "usage: machOconv inputfile [outputfile]\n");
76 exit(1);
77 }
78
79 nc = read(infile, &mh, sizeof (mh));
80 if (nc < 0) {
81 perror("read mach header");
82 exit(1);
83 }
84 if (nc < (int)sizeof (mh)) {
85 fprintf(stderr, "read mach header: premature EOF %d\n", nc);
86 exit(1);
87 }
88 if (mh.magic == MH_MAGIC)
89 swap_ends = FALSE;
90 else if (mh.magic == MH_CIGAM)
91 swap_ends = TRUE;
92 else {
93 fprintf(stderr, "bad magic number %lx\n", mh.magic);
94 exit(1);
95 }
96
97 cmds = calloc(swap(mh.sizeofcmds), sizeof (char));
98 if (cmds == 0) {
99 fprintf(stderr, "alloc load commands: no memory\n");
100 exit(1);
101 }
102 nc = read(infile, cmds, swap(mh.sizeofcmds));
103 if (nc < 0) {
104 perror("read load commands");
105 exit(1);
106 }
107 if (nc < (int)swap(mh.sizeofcmds)) {
108 fprintf(stderr, "read load commands: premature EOF %d\n", nc);
109 exit(1);
110 }
111
112 for ( ncmds = swap(mh.ncmds), cp = cmds;
113 ncmds > 0; ncmds--) {
114 boolean_t isDATA;
115 unsigned vmsize;
116
117 #define lcp ((struct load_command *)cp)
118 switch(swap(lcp->cmd)) {
119
120 case LC_SEGMENT:
121 #define scp ((struct segment_command *)cp)
122 isDATA = (strcmp(scp->segname, "__DATA") == 0);
123 if (isDATA)
124 vmsize = swap(scp->filesize);
125 else
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);
130 exit(1);
131 }
132
133 lseek(infile, swap(scp->fileoff), L_SET);
134 nc = read(infile, (void *)data, swap(scp->filesize));
135 if (nc < 0) {
136 perror("read segment data");
137 exit(1);
138 }
139 if (nc < (int)swap(scp->filesize)) {
140 fprintf(stderr, "read segment data: premature EOF %d\n", nc);
141 exit(1);
142 }
143
144 nc = write(outfile, (void *)data, vmsize);
145 if (nc < (int)vmsize) {
146 perror("write segment data");
147 exit(1);
148 }
149
150 vm_deallocate(mach_task_self(), data, vmsize);
151 break;
152 }
153
154 cp += swap(lcp->cmdsize);
155 }
156
157 exit(0);
158 }