Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2238