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