Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch_Modules/i386/util/segsize.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;
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 intnc, ncmds;
55 char *cp;
56
57 if (argc == 3) {
58infile = open(argv[1], O_RDONLY);
59if (infile < 0)
60goto usage;
61 }
62 else {
63usage:
64 fprintf(stderr, "usage: segsize segment\n");
65exit(1);
66 }
67
68 nc = read(infile, &mh, sizeof (mh));
69 if (nc < 0) {
70perror("read mach header");
71exit(1);
72 }
73 if (nc < (int)sizeof (mh)) {
74fprintf(stderr, "read mach header: premature EOF %d\n", nc);
75exit(1);
76 }
77 if (mh.magic == MH_MAGIC)
78swap_ends = false;
79 else if (mh.magic == MH_CIGAM)
80swap_ends = true;
81 else {
82 fprintf(stderr, "bad magic number %lx\n", (unsigned long)mh.magic);
83exit(1);
84 }
85
86 cmds = calloc(swap(mh.sizeofcmds), sizeof (char));
87 if (cmds == 0) {
88fprintf(stderr, "alloc load commands: no memory\n");
89exit(1);
90 }
91 nc = read(infile, cmds, swap(mh.sizeofcmds));
92 if (nc < 0) {
93perror("read load commands");
94exit(1);
95 }
96 if (nc < (int)swap(mh.sizeofcmds)) {
97fprintf(stderr, "read load commands: premature EOF %d\n", nc);
98exit(1);
99 }
100
101 for (ncmds = swap(mh.ncmds), cp = cmds;
102 ncmds > 0; ncmds--) {
103 // boolisDATA;
104 // unsignedvmsize;
105
106#define lcp((struct load_command *)cp)
107switch(swap(lcp->cmd)) {
108
109case LC_SEGMENT:
110#define scp((struct segment_command *)cp)
111if(strcmp(scp->segname, argv[2]) == 0)
112{
113printf("%ld\n", swap(scp->vmsize));
114#if 0
115if (isDATA)
116vmsize = swap(scp->filesize);
117else
118vmsize = swap(scp->vmsize);
119#endif
120}
121break;
122}
123
124cp += swap(lcp->cmdsize);
125 }
126
127 exit(0);
128}
129

Archive Download this file

Revision: 2238