Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/util/machOconv.c

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

Archive Download this file

Revision: 2073