1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 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 2.0 (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␊ |
12 | * this file.␊ |
13 | * ␊ |
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␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | #include <stdio.h>␊ |
25 | #include <stdlib.h>␊ |
26 | #include <stdbool.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 | static bool␉swap_ends;␊ |
40 | ␊ |
41 | static unsigned long swap(unsigned long x)␊ |
42 | {␊ |
43 | if (swap_ends)␊ |
44 | ␉return OSSwapInt32(x);␊ |
45 | else␊ |
46 | ␉return x;␊ |
47 | }␊ |
48 | ␊ |
49 | int␊ |
50 | main(int argc, char *argv[])␊ |
51 | {␊ |
52 | kern_return_t␉result;␊ |
53 | vm_address_t␉data;␊ |
54 | int␉␉␉nc, ncmds;␊ |
55 | char *␉␉cp;␊ |
56 | ␊ |
57 | ␉if (argc == 2)␊ |
58 | ␉{␊ |
59 | ␉␉infile = open(argv[1], O_RDONLY);␊ |
60 | ␉␉if (infile < 0)␊ |
61 | ␉␉{␊ |
62 | ␉␉␉goto usage;␊ |
63 | ␉␉}␊ |
64 | ␉␉outfile = fileno(stdout);␊ |
65 | ␉}␊ |
66 | ␉else if (argc == 3)␊ |
67 | ␉{␊ |
68 | ␉␉infile = open(argv[1], O_RDONLY);␊ |
69 | ␉␉if (infile < 0){␊ |
70 | ␉␉␉goto usage;␊ |
71 | ␉␉}␊ |
72 | ␉␉outfile = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644);␊ |
73 | ␉␉if (outfile < 0)␊ |
74 | ␉␉{␊ |
75 | ␉␉␉goto usage;␊ |
76 | ␉␉}␊ |
77 | ␉} else {␊ |
78 | usage:␊ |
79 | ␉␉fprintf(stderr, "usage: machOconv inputfile [outputfile]\n");␊ |
80 | ␉␉exit(1);␊ |
81 | ␉}␊ |
82 | ␊ |
83 | ␉nc = read(infile, &mh, sizeof (mh));␊ |
84 | ␉if (nc < 0)␊ |
85 | ␉{␊ |
86 | ␉␉perror("read mach header");␊ |
87 | ␉␉exit(1);␊ |
88 | ␉}␊ |
89 | ␊ |
90 | ␉if (nc < (int)sizeof (mh))␊ |
91 | ␉{␊ |
92 | ␉␉fprintf(stderr, "read mach header: premature EOF %d\n", nc);␊ |
93 | ␉␉exit(1);␊ |
94 | ␉}␊ |
95 | ␊ |
96 | ␉if (mh.magic == MH_MAGIC)␊ |
97 | ␉{␊ |
98 | ␉␉swap_ends = false;␊ |
99 | ␉}␊ |
100 | ␉else if (mh.magic == MH_CIGAM)␊ |
101 | ␉{␊ |
102 | ␉␉swap_ends = true;␊ |
103 | ␉} else {␊ |
104 | ␉␉fprintf(stderr, "bad magic number %lx\n", (unsigned long)mh.magic);␊ |
105 | ␉␉exit(1);␊ |
106 | ␉}␊ |
107 | ␊ |
108 | ␉cmds = calloc(swap(mh.sizeofcmds), sizeof (char));␊ |
109 | ␉if (cmds == 0)␊ |
110 | ␉{␊ |
111 | ␉␉fprintf(stderr, "alloc load commands: no memory\n");␊ |
112 | ␉␉exit(1);␊ |
113 | ␉}␊ |
114 | ␊ |
115 | ␉nc = read(infile, cmds, swap(mh.sizeofcmds));␊ |
116 | ␉if (nc < 0) ␊ |
117 | ␉{␊ |
118 | ␉␉perror("read load commands");␊ |
119 | ␉␉exit(1);␊ |
120 | ␉}␊ |
121 | ␉if (nc < (int)swap(mh.sizeofcmds))␊ |
122 | ␉{␊ |
123 | ␉␉fprintf(stderr, "read load commands: premature EOF %d\n", nc);␊ |
124 | ␉␉exit(1);␊ |
125 | ␉}␊ |
126 | ␊ |
127 | ␉unsigned long vmstart = (unsigned long)-1;␊ |
128 | ␊ |
129 | ␉// First pass: determine actual load address␊ |
130 | ␉for (ncmds = swap(mh.ncmds), cp = cmds; ncmds > 0; ncmds--)␊ |
131 | ␉{␊ |
132 | #define lcp␉((struct load_command *)cp)␊ |
133 | #define scp␉((struct segment_command *)cp)␊ |
134 | ␊ |
135 | ␉␉switch(swap(lcp->cmd))␊ |
136 | ␉␉{␊ |
137 | ␉␉␉case LC_SEGMENT:␊ |
138 | ␉␉␉␉if(vmstart > swap(scp->vmaddr)) ␊ |
139 | ␉␉␉␉{␊ |
140 | ␉␉␉␉␉vmstart = swap(scp->vmaddr);␊ |
141 | ␉␉␉␉}␊ |
142 | ␉␉}␊ |
143 | ␉␉cp += swap(lcp->cmdsize);␊ |
144 | ␊ |
145 | ␉}␊ |
146 | ␊ |
147 | ␉// Second pass: output to file.␊ |
148 | ␉for (ncmds = swap(mh.ncmds), cp = cmds; ncmds > 0; ncmds--)␊ |
149 | ␉{␊ |
150 | #define lcp␉((struct load_command *)cp)␊ |
151 | #define scp␉((struct segment_command *)cp)␊ |
152 | ␊ |
153 | ␉␉bool␉isDATA;␊ |
154 | ␉␉unsigned␉vmsize;␊ |
155 | ␊ |
156 | ␉␉switch(swap(lcp->cmd))␊ |
157 | ␉␉{␊ |
158 | ␉␉␉case LC_SEGMENT:␊ |
159 | ␉␉␉␉isDATA = (strcmp(scp->segname, "__DATA") == 0);␊ |
160 | ␉␉␉␉if (isDATA)␊ |
161 | ␉␉␉␉{␊ |
162 | ␉␉␉␉␉vmsize = swap(scp->filesize);␊ |
163 | ␉␉␉␉} else {␊ |
164 | ␉␉␉␉␉vmsize = swap(scp->vmsize);␊ |
165 | ␉␉␉␉}␊ |
166 | ␊ |
167 | ␉␉␉␉result = vm_allocate(mach_task_self(), &data, vmsize, true);␊ |
168 | ␉␉␉␉if (result != KERN_SUCCESS)␊ |
169 | ␉␉␉␉{␊ |
170 | ␉␉␉␉␉mach_error("vm_allocate segment data", result);␊ |
171 | ␉␉␉␉␉exit(1);␊ |
172 | ␉␉␉␉}␊ |
173 | ␊ |
174 | ␉␉␉␉lseek(infile, swap(scp->fileoff), L_SET);␊ |
175 | ␉␉␉␉nc = read(infile, (void *)data, swap(scp->filesize));␊ |
176 | ␉␉␉␉if (nc < 0) {␊ |
177 | ␉␉␉␉␉perror("read segment data");␊ |
178 | ␉␉␉␉␉exit(1);␊ |
179 | ␉␉␉␉}␊ |
180 | ␉␉␉␉if (nc < (int)swap(scp->filesize))␊ |
181 | ␉␉␉␉{␊ |
182 | ␉␉␉␉␉fprintf(stderr, "read segment data: premature EOF %d\n", nc);␊ |
183 | ␉␉␉␉␉exit(1);␊ |
184 | ␉␉␉␉}␊ |
185 | ␊ |
186 | ␉␉␉␉lseek(outfile, swap(scp->vmaddr) - vmstart, L_SET);␊ |
187 | ␉␉␉␉nc = write(outfile, (void *)data, vmsize);␊ |
188 | ␉␉␉␉if (nc < (int)vmsize)␊ |
189 | ␉␉␉␉{␊ |
190 | ␉␉␉␉␉perror("write segment data");␊ |
191 | ␉␉␉␉␉exit(1);␊ |
192 | ␉␉␉␉}␊ |
193 | ␊ |
194 | ␉␉␉␉vm_deallocate(mach_task_self(), data, vmsize);␊ |
195 | ␉␉␉␉break;␊ |
196 | ␉␉}␊ |
197 | ␊ |
198 | ␉␉cp += swap(lcp->cmdsize);␊ |
199 | ␉}␊ |
200 | ␉␊ |
201 | ␉exit(0);␊ |
202 | }␊ |
203 | |