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