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

Archive Download this file

Revision: 2476