Chameleon

Chameleon Svn Source Tree

Root/branches/valv/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(
42 unsigned long x
43)
44{
45 if (swap_ends)
46return OSSwapInt32(x);
47 else
48return x;
49}
50
51int
52main(int argc, char *argv[])
53{
54 kern_return_tresult;
55 vm_address_tdata;
56 intnc, ncmds;
57 char *cp;
58
59 if (argc == 2) {
60infile = open(argv[1], O_RDONLY);
61if (infile < 0)
62 goto usage;
63outfile = fileno(stdout);
64 }
65 else if (argc == 3) {
66 infile = open(argv[1], O_RDONLY);
67if (infile < 0)
68 goto usage;
69outfile = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644);
70if (outfile < 0)
71 goto usage;
72 }
73 else {
74usage:
75 fprintf(stderr, "usage: machOconv inputfile [outputfile]\n");
76exit(1);
77 }
78
79 nc = read(infile, &mh, sizeof (mh));
80 if (nc < 0) {
81perror("read mach header");
82exit(1);
83 }
84 if (nc < (int)sizeof (mh)) {
85fprintf(stderr, "read mach header: premature EOF %d\n", nc);
86exit(1);
87 }
88 if (mh.magic == MH_MAGIC)
89swap_ends = false;
90 else if (mh.magic == MH_CIGAM)
91swap_ends = true;
92 else {
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) {
99fprintf(stderr, "alloc load commands: no memory\n");
100exit(1);
101 }
102 nc = read(infile, cmds, swap(mh.sizeofcmds));
103 if (nc < 0) {
104perror("read load commands");
105exit(1);
106 }
107 if (nc < (int)swap(mh.sizeofcmds)) {
108fprintf(stderr, "read load commands: premature EOF %d\n", nc);
109exit(1);
110 }
111
112 for (ncmds = swap(mh.ncmds), cp = cmds;
113ncmds > 0; ncmds--) {
114 boolisDATA;
115 unsignedvmsize;
116
117#define lcp((struct load_command *)cp)
118switch(swap(lcp->cmd)) {
119
120case LC_SEGMENT:
121#define scp((struct segment_command *)cp)
122 isDATA = (strcmp(scp->segname, "__DATA") == 0);
123 if (isDATA)
124 vmsize = swap(scp->filesize);
125 else
126 vmsize = swap(scp->vmsize);
127 result = vm_allocate(mach_task_self(), &data, vmsize, true);
128 if (result != KERN_SUCCESS) {
129mach_error("vm_allocate segment data", result);
130exit(1);
131 }
132
133 lseek(infile, swap(scp->fileoff), L_SET);
134 nc = read(infile, (void *)data, swap(scp->filesize));
135 if (nc < 0) {
136perror("read segment data");
137exit(1);
138 }
139 if (nc < (int)swap(scp->filesize)) {
140fprintf(stderr, "read segment data: premature EOF %d\n", nc);
141exit(1);
142 }
143
144 nc = write(outfile, (void *)data, vmsize);
145 if (nc < (int)vmsize) {
146perror("write segment data");
147exit(1);
148 }
149
150 vm_deallocate(mach_task_self(), data, vmsize);
151 break;
152}
153
154cp += swap(lcp->cmdsize);
155 }
156
157 exit(0);
158}
159

Archive Download this file

Revision: 164