Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Trunk/i386/util/machOconv.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 *
5 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
6 * Reserved. This file contains Original Code and/or Modifications of
7 * Original Code as defined in and that are subject to the Apple Public
8 * Source License Version 2.0 (the "License"). You may not use this file
9 * except in compliance with the License. Please obtain a copy of the
10 * License at http://www.apple.com/publicsource and read it before using
11 * this file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
18 * License for the specific language governing rights and limitations
19 * under the License.
20 *
21 */
22#include <stdio.h>
23#include <stdlib.h>
24#include <stdbool.h>
25#include <mach/mach.h>
26#include <mach/mach_error.h>
27#include <sys/file.h>
28#include <mach-o/loader.h>
29#include <libkern/OSByteOrder.h>
30#include <unistd.h>
31
32intinfile, outfile;
33
34struct mach_headermh;
35void *cmds;
36
37static boolswap_ends;
38
39static unsigned long swap(
40 unsigned long x
41)
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
110 for (ncmds = swap(mh.ncmds), cp = cmds;
111ncmds > 0; ncmds--) {
112 boolisDATA;
113 unsignedvmsize;
114
115#define lcp((struct load_command *)cp)
116switch(swap(lcp->cmd)) {
117
118case LC_SEGMENT:
119#define scp((struct segment_command *)cp)
120 isDATA = (strcmp(scp->segname, "__DATA") == 0);
121 if (isDATA)
122 vmsize = swap(scp->filesize);
123 else
124 vmsize = swap(scp->vmsize);
125 result = vm_allocate(mach_task_self(), &data, vmsize, true);
126 if (result != KERN_SUCCESS) {
127mach_error("vm_allocate segment data", result);
128exit(1);
129 }
130
131 lseek(infile, swap(scp->fileoff), L_SET);
132 nc = read(infile, (void *)data, swap(scp->filesize));
133 if (nc < 0) {
134perror("read segment data");
135exit(1);
136 }
137 if (nc < (int)swap(scp->filesize)) {
138fprintf(stderr, "read segment data: premature EOF %d\n", nc);
139exit(1);
140 }
141
142 nc = write(outfile, (void *)data, vmsize);
143 if (nc < (int)vmsize) {
144perror("write segment data");
145exit(1);
146 }
147
148 vm_deallocate(mach_task_self(), data, vmsize);
149 break;
150}
151
152cp += swap(lcp->cmdsize);
153 }
154
155 exit(0);
156}
157

Archive Download this file

Revision: 2045