]> git.saurik.com Git - apple/objc4.git/blob - markgc.c
objc4-493.9.tar.gz
[apple/objc4.git] / markgc.c
1 /*
2 * Copyright (c) 2007-2009 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * 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, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #import <stdio.h>
28 #include <fcntl.h>
29 #import <sys/stat.h>
30 #import <mach-o/fat.h>
31 #import <mach-o/arch.h>
32 #import <mach-o/loader.h>
33
34 // from "objc-private.h"
35 // masks for objc_image_info.flags
36 #define OBJC_IMAGE_IS_REPLACEMENT (1<<0)
37 #define OBJC_IMAGE_SUPPORTS_GC (1<<1)
38 #define OBJC_IMAGE_REQUIRES_GC (1<<2)
39 #define OBJC_IMAGE_OPTIMIZED_BY_DYLD (1<<3)
40 #define OBJC_IMAGE_SUPPORTS_COMPACTION (1<<4)
41
42 typedef char bool;
43 #define true 1
44 #define false 0
45
46 bool debug;
47 bool verbose;
48 bool quiet;
49 bool rrOnly;
50 bool patch = true;
51 bool unpatch = false;
52
53 struct gcinfo {
54 bool hasObjC;
55 bool hasInfo;
56 uint32_t flags;
57 char *arch;
58 } GCInfo[4];
59
60 void dumpinfo(char *filename);
61
62 int Errors = 0;
63 char *FileBase;
64 size_t FileSize;
65 const char *FileName;
66
67 int main(int argc, char *argv[]) {
68 //NSAutoreleasePool *pool = [NSAutoreleasePool new];
69 int i;
70 //dumpinfo("/System/Library/Frameworks/AppKit.framework/AppKit");
71 if (argc == 1) {
72 printf("Usage: markgc [-v] [-r] [--] library_or_executable_image [image2 ...]\n");
73 printf(" changes Garbage Collection readiness of named images, ignoring those without ObjC segments\n");
74 printf(" -p - patch RR binary to (apparently) support GC (default)\n");
75 printf(" -u - unpatch GC binary to RR only\n");
76 printf("\nAuthor: blaine@apple.com\n");
77 exit(0);
78 }
79 for (i = 1; i < argc; ++i) {
80 if (!strcmp(argv[i], "-v")) {
81 verbose = true;
82 continue;
83 }
84 if (!strcmp(argv[i], "-d")) {
85 debug = true;
86 continue;
87 }
88 if (!strcmp(argv[i], "-q")) {
89 quiet = true;
90 continue;
91 }
92 if (!strcmp(argv[i], "-p")) {
93 patch = true;
94 continue;
95 }
96 if (!strcmp(argv[i], "-u")) {
97 unpatch = true;
98 patch = false;
99 continue;
100 }
101 dumpinfo(argv[i]);
102 }
103 return Errors;
104 }
105
106 struct imageInfo {
107 uint32_t version;
108 uint32_t flags;
109 };
110
111 void patchFile(uint32_t value, size_t offset) {
112 int fd = open(FileName, 1);
113 off_t lresult = lseek(fd, offset, SEEK_SET);
114 if (lresult == -1) {
115 printf("couldn't seek to 0x%lx position on fd %d\n", offset, fd);
116 ++Errors;
117 return;
118 }
119 size_t wresult = write(fd, &value, 4);
120 if (wresult != 4) {
121 ++Errors;
122 printf("didn't write new value\n");
123 }
124 else {
125 printf("patched %s at offset 0x%lx\n", FileName, offset);
126 }
127 close(fd);
128 }
129
130 uint32_t iiflags(struct imageInfo *ii, size_t size, bool needsFlip) {
131 if (needsFlip) {
132 ii->flags = OSSwapInt32(ii->flags);
133 }
134 if (debug) printf("flags->%x, nitems %lu\n", ii->flags, size/sizeof(struct imageInfo));
135 uint32_t support_mask = (OBJC_IMAGE_SUPPORTS_GC | OBJC_IMAGE_SUPPORTS_COMPACTION);
136 uint32_t flags = ii->flags;
137 if (patch && (flags & support_mask) != support_mask) {
138 //printf("will patch %s at offset %p\n", FileName, (char*)(&ii->flags) - FileBase);
139 uint32_t newvalue = flags | support_mask;
140 if (needsFlip) newvalue = OSSwapInt32(newvalue);
141 patchFile(newvalue, (char*)(&ii->flags) - FileBase);
142 }
143 if (unpatch && (flags & support_mask) == support_mask) {
144 uint32_t newvalue = flags & ~support_mask;
145 if (needsFlip) newvalue = OSSwapInt32(newvalue);
146 patchFile(newvalue, (char*)(&ii->flags) - FileBase);
147 }
148 for(int niis = 1; niis < size/sizeof(struct imageInfo); ++niis) {
149 if (needsFlip) ii[niis].flags = OSSwapInt32(ii[niis].flags);
150 if (ii[niis].flags != flags) {
151 // uh, oh.
152 printf("XXX ii[%d].flags %x != ii[0].flags %x\n", niis, ii[niis].flags, flags);
153 ++Errors;
154 }
155 }
156 return flags;
157 }
158
159 void printflags(uint32_t flags) {
160 if (flags & 0x1) printf(" F&C");
161 if (flags & 0x2) printf(" GC");
162 if (flags & 0x4) printf(" GC-only");
163 else printf(" RR");
164 }
165
166 /*
167 void doimageinfo(struct imageInfo *ii, uint32_t size, bool needsFlip) {
168 uint32_t flags = iiflags(ii, size, needsFlip);
169 printflags(flags);
170 }
171 */
172
173
174 void dosect32(void *start, struct section *sect, bool needsFlip, struct gcinfo *gcip) {
175 if (debug) printf("section %s from segment %s\n", sect->sectname, sect->segname);
176 if (strcmp(sect->segname, "__OBJC")) return;
177 gcip->hasObjC = true;
178 if (strcmp(sect->sectname, "__image_info")) return;
179 gcip->hasInfo = true;
180 if (needsFlip) {
181 sect->offset = OSSwapInt32(sect->offset);
182 sect->size = OSSwapInt32(sect->size);
183 }
184 // these guys aren't inline - they point elsewhere
185 gcip->flags = iiflags(start + sect->offset, sect->size, needsFlip);
186 }
187
188 void dosect64(void *start, struct section_64 *sect, bool needsFlip, struct gcinfo *gcip) {
189 if (debug) printf("section %s from segment %s\n", sect->sectname, sect->segname);
190 if (strcmp(sect->segname, "__OBJC") && strcmp(sect->segname, "__DATA")) return;
191 if (strcmp(sect->sectname, "__image_info") && strncmp(sect->sectname, "__objc_imageinfo", 16)) return;
192 gcip->hasObjC = true;
193 gcip->hasInfo = true;
194 if (needsFlip) {
195 sect->offset = OSSwapInt32(sect->offset);
196 sect->size = OSSwapInt64(sect->size);
197 }
198 // these guys aren't inline - they point elsewhere
199 gcip->flags = iiflags(start + sect->offset, (size_t)sect->size, needsFlip);
200 }
201
202 void doseg32(void *start, struct segment_command *seg, bool needsFlip, struct gcinfo *gcip) {
203 // lets do sections
204 if (needsFlip) {
205 seg->fileoff = OSSwapInt32(seg->fileoff);
206 seg->nsects = OSSwapInt32(seg->nsects);
207 }
208 if (debug) printf("segment name: %s, nsects %d\n", seg->segname, seg->nsects);
209 if (seg->segname[0]) {
210 if (strcmp("__OBJC", seg->segname)) return;
211 }
212 struct section *sect = (struct section *)(seg + 1);
213 for (uint32_t nsects = 0; nsects < seg->nsects; ++nsects) {
214 // sections directly follow
215
216 dosect32(start, sect + nsects, needsFlip, gcip);
217 }
218 }
219 void doseg64(void *start, struct segment_command_64 *seg, bool needsFlip, struct gcinfo *gcip) {
220 if (debug) printf("segment name: %s\n", seg->segname);
221 if (seg->segname[0] && strcmp("__OBJC", seg->segname) && strcmp("__DATA", seg->segname)) return;
222 gcip->hasObjC = true;
223 // lets do sections
224 if (needsFlip) {
225 seg->fileoff = OSSwapInt64(seg->fileoff);
226 seg->nsects = OSSwapInt32(seg->nsects);
227 }
228 struct section_64 *sect = (struct section_64 *)(seg + 1);
229 for (uint32_t nsects = 0; nsects < seg->nsects; ++nsects) {
230 // sections directly follow
231
232 dosect64(start, sect + nsects, needsFlip, gcip);
233 }
234 }
235
236 #if 0
237 /*
238 * A variable length string in a load command is represented by an lc_str
239 * union. The strings are stored just after the load command structure and
240 * the offset is from the start of the load command structure. The size
241 * of the string is reflected in the cmdsize field of the load command.
242 * Once again any padded bytes to bring the cmdsize field to a multiple
243 * of 4 bytes must be zero.
244 */
245 union lc_str {
246 uint32_t offset; /* offset to the string */
247 #ifndef __LP64__
248 char *ptr; /* pointer to the string */
249 #endif
250 };
251
252 struct dylib {
253 union lc_str name; /* library's path name */
254 uint32_t timestamp; /* library's build time stamp */
255 uint32_t current_version; /* library's current version number */
256 uint32_t compatibility_version; /* library's compatibility vers number*/
257 };
258
259 * A dynamically linked shared library (filetype == MH_DYLIB in the mach header)
260 * contains a dylib_command (cmd == LC_ID_DYLIB) to identify the library.
261 * An object that uses a dynamically linked shared library also contains a
262 * dylib_command (cmd == LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, or
263 * LC_REEXPORT_DYLIB) for each library it uses.
264
265 struct dylib_command {
266 uint32_t cmd; /* LC_ID_DYLIB, LC_LOAD_{,WEAK_}DYLIB,
267 LC_REEXPORT_DYLIB */
268 uint32_t cmdsize; /* includes pathname string */
269 struct dylib dylib; /* the library identification */
270 };
271 #endif
272
273 void dodylib(void *start, struct dylib_command *dylibCmd, bool needsFlip) {
274 if (!verbose) return;
275 if (needsFlip) {
276 }
277 size_t count = dylibCmd->cmdsize - sizeof(struct dylib_command);
278 //printf("offset is %d, count is %d\n", dylibCmd->dylib.name.offset, count);
279 if (dylibCmd->dylib.name.offset > count) return;
280 //printf("-->%.*s<---", count, ((void *)dylibCmd)+dylibCmd->dylib.name.offset);
281 if (verbose) printf("load %s\n", ((char *)dylibCmd)+dylibCmd->dylib.name.offset);
282 }
283
284 struct load_command *doloadcommand(void *start, struct load_command *lc, bool needsFlip, bool is32, struct gcinfo *gcip) {
285 if (needsFlip) {
286 lc->cmd = OSSwapInt32(lc->cmd);
287 lc->cmdsize = OSSwapInt32(lc->cmdsize);
288 }
289
290 switch(lc->cmd) {
291 case LC_SEGMENT_64:
292 if (debug) printf("...segment64\n");
293 if (is32) printf("XXX we have a 64-bit segment in a 32-bit mach-o\n");
294 doseg64(start, (struct segment_command_64 *)lc, needsFlip, gcip);
295 break;
296 case LC_SEGMENT:
297 if (debug) printf("...segment32\n");
298 doseg32(start, (struct segment_command *)lc, needsFlip, gcip);
299 break;
300 case LC_SYMTAB: if (debug) printf("...dynamic symtab\n"); break;
301 case LC_DYSYMTAB: if (debug) printf("...symtab\n"); break;
302 case LC_LOAD_DYLIB:
303 dodylib(start, (struct dylib_command *)lc, needsFlip);
304 break;
305 case LC_SUB_UMBRELLA: if (debug) printf("...load subumbrella\n"); break;
306 default: if (debug) printf("cmd is %x\n", lc->cmd); break;
307 }
308
309 return (struct load_command *)((void *)lc + lc->cmdsize);
310 }
311
312 void doofile(void *start, size_t size, struct gcinfo *gcip) {
313 struct mach_header *mh = (struct mach_header *)start;
314 bool isFlipped = false;
315 if (mh->magic == MH_CIGAM || mh->magic == MH_CIGAM_64) {
316 if (debug) printf("(flipping)\n");
317 mh->magic = OSSwapInt32(mh->magic);
318 mh->cputype = OSSwapInt32(mh->cputype);
319 mh->cpusubtype = OSSwapInt32(mh->cpusubtype);
320 mh->filetype = OSSwapInt32(mh->filetype);
321 mh->ncmds = OSSwapInt32(mh->ncmds);
322 mh->sizeofcmds = OSSwapInt32(mh->sizeofcmds);
323 mh->flags = OSSwapInt32(mh->flags);
324 isFlipped = true;
325 }
326 if (rrOnly && mh->filetype != MH_DYLIB) return; // ignore executables
327 NXArchInfo *info = (NXArchInfo *)NXGetArchInfoFromCpuType(mh->cputype, mh->cpusubtype);
328 //printf("%s:", info->description);
329 gcip->arch = (char *)info->description;
330 //if (debug) printf("...description is %s\n", info->description);
331 bool is32 = !(mh->cputype & CPU_ARCH_ABI64);
332 if (debug) printf("is 32? %d\n", is32);
333 if (debug) printf("filetype -> %d\n", mh->filetype);
334 if (debug) printf("ncmds -> %d\n", mh->ncmds);
335 struct load_command *lc = (is32 ? (struct load_command *)(mh + 1) : (struct load_command *)((struct mach_header_64 *)start + 1));
336 int ncmds;
337 for (ncmds = 0; ncmds < mh->ncmds; ++ncmds) {
338 lc = doloadcommand(start, lc, isFlipped, is32, gcip);
339 }
340 //printf("\n");
341 }
342
343 void initGCInfo() {
344 bzero((void *)GCInfo, sizeof(GCInfo));
345 }
346
347 void printGCInfo(char *filename) {
348 if (!GCInfo[0].hasObjC) return; // don't bother
349 // verify that flags are all the same
350 uint32_t flags = GCInfo[0].flags;
351 bool allSame = true;
352 for (int i = 1; i < 4 && GCInfo[i].arch; ++i) {
353 if (flags != GCInfo[i].flags) {
354 allSame = false;
355 }
356 }
357 if (rrOnly) {
358 if (allSame && (flags & 0x2))
359 return;
360 printf("*** not all GC in %s:\n", filename);
361 }
362 if (allSame && !verbose) {
363 printf("%s:", filename);
364 printflags(flags);
365 printf("\n");
366 }
367 else {
368 printf("%s:\n", filename);
369 for (int i = 0; i < 4 && GCInfo[i].arch; ++i) {
370 printf("%s:", GCInfo[i].arch);
371 printflags(GCInfo[i].flags);
372 printf("\n");
373 }
374 printf("\n");
375 }
376 }
377
378 void dofat(void *start) {
379 struct fat_header *fh = start;
380 bool needsFlip = false;
381 if (fh->magic == FAT_CIGAM) {
382 fh->nfat_arch = OSSwapInt32(fh->nfat_arch);
383 needsFlip = true;
384 }
385 if (debug) printf("%d architectures\n", fh->nfat_arch);
386 int narchs;
387 struct fat_arch *arch_ptr = (struct fat_arch *)(fh + 1);
388 for (narchs = 0; narchs < fh->nfat_arch; ++narchs) {
389 if (debug) printf("doing arch %d\n", narchs);
390 if (needsFlip) {
391 arch_ptr->offset = OSSwapInt32(arch_ptr->offset);
392 arch_ptr->size = OSSwapInt32(arch_ptr->size);
393 }
394 doofile(start+arch_ptr->offset, arch_ptr->size, &GCInfo[narchs]);
395 arch_ptr++;
396 }
397 }
398
399 bool openFile(const char *filename) {
400 FileName = filename;
401 // get size
402 struct stat statb;
403 int fd = open(filename, 0);
404 if (fd < 0) {
405 printf("couldn't open %s for reading\n", filename);
406 return false;
407 }
408 int osresult = fstat(fd, &statb);
409 if (osresult != 0) {
410 printf("couldn't get size of %s\n", filename);
411 close(fd);
412 return false;
413 }
414 FileSize = (size_t)statb.st_size;
415 FileBase = malloc(FileSize);
416 if (!FileBase) {
417 printf("couldn't malloc %lu bytes\n", FileSize);
418 close(fd);
419 return false;
420 }
421 off_t readsize = read(fd, FileBase, FileSize);
422 if (readsize != FileSize) {
423 printf("read %ld bytes, wanted %ld\n", (size_t)readsize, FileSize);
424 close(fd);
425 return false;
426 }
427 close(fd);
428 return true;
429 }
430
431 void closeFile() {
432 free(FileBase);
433 }
434
435 void dumpinfo(char *filename) {
436 initGCInfo();
437 if (!openFile(filename)) exit(1);
438 struct fat_header *fh = (struct fat_header *)FileBase;
439 if (fh->magic == FAT_MAGIC || fh->magic == FAT_CIGAM) {
440 dofat((void *)FileBase);
441 //printGCInfo(filename);
442 }
443 else if (fh->magic == MH_MAGIC || fh->magic == MH_CIGAM || fh->magic == MH_MAGIC_64 || fh->magic == MH_CIGAM_64) {
444 doofile((void *)FileBase, FileSize, &GCInfo[0]);
445 //printGCInfo(filename);
446 }
447 else if (!quiet) {
448 printf("don't understand %s!\n", filename);
449 }
450 closeFile();
451 }
452