]> git.saurik.com Git - apple/boot.git/blame - gen/util/machOconv.c
boot-122.tar.gz
[apple/boot.git] / gen / util / machOconv.c
CommitLineData
14c7c974
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
4f6e3300
A
6 * Portions Copyright (c) 1999 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 1.1 (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.
14c7c974
A
13 *
14 * The Original Code and all software distributed under the License are
4f6e3300 15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14c7c974
A
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
4f6e3300
A
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.
14c7c974
A
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24#include <stdio.h>
25
26#include <mach/mach.h>
27#include <sys/file.h>
28#include <mach-o/loader.h>
f083c6c3 29#include <libkern/OSByteOrder.h>
14c7c974
A
30
31int infile, outfile;
32
33struct mach_header mh;
34unsigned cmds;
35
36boolean_t swap_ends;
37
38static unsigned long swap(
39 unsigned long x
40)
41{
42 if (swap_ends)
f083c6c3 43 return OSSwapInt32(x);
14c7c974
A
44 else
45 return x;
46}
47
48main(argc, argv)
49int argc;
50char *argv[];
51{
52 kern_return_t result;
53 vm_address_t data;
54 int nc, ncmds;
55 unsigned cp;
56
57 if (argc == 2) {
58 infile = open(argv[1], O_RDONLY);
59 if (infile < 0)
60 goto usage;
61 outfile = fileno(stdout);
62 }
63 else if (argc == 3) {
64 infile = open(argv[1], O_RDONLY);
65 if (infile < 0)
66 goto usage;
67 outfile = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644);
68 if (outfile < 0)
69 goto usage;
70 }
71 else {
72usage:
73 fprintf(stderr, "usage: machOconv inputfile [outputfile]\n");
74 exit(1);
75 }
76
77 nc = read(infile, &mh, sizeof (mh));
78 if (nc < 0) {
79 perror("read mach header");
80 exit(1);
81 }
82 if (nc < sizeof (mh)) {
83 fprintf(stderr, "read mach header: premature EOF %d\n", nc);
84 exit(1);
85 }
86 if (mh.magic == MH_MAGIC)
87 swap_ends = FALSE;
88 else if (mh.magic == MH_CIGAM)
89 swap_ends = TRUE;
90 else {
91 fprintf(stderr, "bad magic number %x\n", mh.magic);
92 exit(1);
93 }
94
95 cmds = calloc(swap(mh.sizeofcmds), sizeof (char));
96 if (cmds == 0) {
97 fprintf(stderr, "alloc load commands: no memory\n");
98 exit(1);
99 }
100 nc = read(infile, cmds, swap(mh.sizeofcmds));
101 if (nc < 0) {
102 perror("read load commands");
103 exit(1);
104 }
105 if (nc < swap(mh.sizeofcmds)) {
106 fprintf(stderr, "read load commands: premature EOF %d\n", nc);
107 exit(1);
108 }
109
110 for ( ncmds = swap(mh.ncmds), cp = cmds;
111 ncmds > 0; ncmds--) {
112 boolean_t isDATA;
113 unsigned vmsize;
114
115#define lcp ((struct load_command *)cp)
116 switch(swap(lcp->cmd)) {
117
118 case 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) {
127 mach_error("vm_allocate segment data", result);
128 exit(1);
129 }
130
131 b_lseek(infile, swap(scp->fileoff), L_SET);
132 nc = read(infile, data, swap(scp->filesize));
133 if (nc < 0) {
134 perror("read segment data");
135 exit(1);
136 }
137 if (nc < swap(scp->filesize)) {
138 fprintf(stderr, "read segment data: premature EOF %d\n", nc);
139 exit(1);
140 }
141
142 nc = write(outfile, data, vmsize);
143 if (nc < vmsize) {
144 perror("write segment data");
145 exit(1);
146 }
147
148 vm_deallocate(mach_task_self(), data, vmsize);
149 break;
150 }
151
152 cp += swap(lcp->cmdsize);
153 }
154
155 exit(0);
156}