Chameleon

Chameleon Svn Source Tree

Root/branches/chucko/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
82 if (nc < (int)sizeof (mh))
83{
84fprintf(stderr, "read mach header: premature EOF %d\n", nc);
85exit(1);
86 }
87
88
89 if (mh.magic == MH_MAGIC)swap_ends = false;
90 else if (mh.magic == MH_CIGAM)swap_ends = true;
91 else
92{
93 fprintf(stderr, "bad magic number %lx\n", (unsigned long)mh.magic);
94exit(1);
95 }
96
97 cmds = calloc(swap(mh.sizeofcmds), sizeof (char));
98 if (cmds == 0)
99{
100fprintf(stderr, "alloc load commands: no memory\n");
101exit(1);
102 }
103 nc = read(infile, cmds, swap(mh.sizeofcmds));
104 if (nc < 0)
105{
106perror("read load commands");
107exit(1);
108 }
109 if (nc < (int)swap(mh.sizeofcmds))
110{
111fprintf(stderr, "read load commands: premature EOF %d\n", nc);
112exit(1);
113 }
114
115unsigned long vmstart = (unsigned long)-1;
116
117// First pass: determine actual load address
118for (ncmds = swap(mh.ncmds), cp = cmds;
119 ncmds > 0; ncmds--)
120{
121#define lcp((struct load_command *)cp)
122#define scp((struct segment_command *)cp)
123
124switch(swap(lcp->cmd))
125{
126case LC_SEGMENT:
127if(vmstart > swap(scp->vmaddr))
128{
129vmstart = swap(scp->vmaddr);
130}
131}
132cp += swap(lcp->cmdsize);
133
134}
135
136// Second pass: output to file.
137 for (ncmds = swap(mh.ncmds), cp = cmds;
138 ncmds > 0; ncmds--)
139{
140#define lcp((struct load_command *)cp)
141#define scp((struct segment_command *)cp)
142
143 boolisDATA;
144 unsignedvmsize;
145
146switch(swap(lcp->cmd))
147{
148case LC_SEGMENT:
149isDATA = (strcmp(scp->segname, "__DATA") == 0);
150if (isDATA)
151{
152vmsize = swap(scp->filesize);
153}
154else
155{
156vmsize = swap(scp->vmsize);
157}
158
159result = vm_allocate(mach_task_self(), &data, vmsize, true);
160if (result != KERN_SUCCESS) {
161mach_error("vm_allocate segment data", result);
162exit(1);
163}
164
165lseek(infile, swap(scp->fileoff), L_SET);
166nc = read(infile, (void *)data, swap(scp->filesize));
167if (nc < 0) {
168perror("read segment data");
169exit(1);
170}
171if (nc < (int)swap(scp->filesize)) {
172fprintf(stderr, "read segment data: premature EOF %d\n", nc);
173exit(1);
174}
175
176lseek(outfile, swap(scp->vmaddr) - vmstart, L_SET);
177nc = write(outfile, (void *)data, vmsize);
178if (nc < (int)vmsize) {
179perror("write segment data");
180exit(1);
181}
182
183vm_deallocate(mach_task_self(), data, vmsize);
184break;
185}
186
187cp += swap(lcp->cmdsize);
188 }
189
190 exit(0);
191}
192

Archive Download this file

Revision: HEAD