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