]>
git.saurik.com Git - apple/boot.git/blob - i386/util/machOconv.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
26 #include <mach/mach.h>
28 #include <mach-o/loader.h>
29 #include <architecture/byte_order.h>
33 struct mach_header mh
;
38 static unsigned long swap(
58 infile
= open(argv
[1], O_RDONLY
);
61 outfile
= fileno(stdout
);
64 infile
= open(argv
[1], O_RDONLY
);
67 outfile
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
73 fprintf(stderr
, "usage: machOconv inputfile [outputfile]\n");
77 nc
= read(infile
, &mh
, sizeof (mh
));
79 perror("read mach header");
82 if (nc
< sizeof (mh
)) {
83 fprintf(stderr
, "read mach header: premature EOF %d\n", nc
);
86 if (mh
.magic
== MH_MAGIC
)
88 else if (mh
.magic
== MH_CIGAM
)
91 fprintf(stderr
, "bad magic number %x\n", mh
.magic
);
95 cmds
= calloc(swap(mh
.sizeofcmds
), sizeof (char));
97 fprintf(stderr
, "alloc load commands: no memory\n");
100 nc
= read(infile
, cmds
, swap(mh
.sizeofcmds
));
102 perror("read load commands");
105 if (nc
< swap(mh
.sizeofcmds
)) {
106 fprintf(stderr
, "read load commands: premature EOF %d\n", nc
);
110 for ( ncmds
= swap(mh
.ncmds
), cp
= cmds
;
111 ncmds
> 0; ncmds
--) {
115 #define lcp ((struct load_command *)cp)
116 switch(swap(lcp
->cmd
)) {
119 #define scp ((struct segment_command *)cp)
120 isDATA
= (strcmp(scp
->segname
, "__DATA") == 0);
122 vmsize
= swap(scp
->filesize
);
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
);
131 lseek(infile
, swap(scp
->fileoff
), L_SET
);
132 nc
= read(infile
, data
, swap(scp
->filesize
));
134 perror("read segment data");
137 if (nc
< swap(scp
->filesize
)) {
138 fprintf(stderr
, "read segment data: premature EOF %d\n", nc
);
142 nc
= write(outfile
, data
, vmsize
);
144 perror("write segment data");
148 vm_deallocate(mach_task_self(), data
, vmsize
);
152 cp
+= swap(lcp
->cmdsize
);