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