2 * Copyright (c) 2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
30 #import <mach-o/fat.h>
31 #import <mach-o/arch.h>
32 #import <mach-o/loader.h>
51 void dumpinfo(char *filename
);
58 int main(int argc
, char *argv
[]) {
59 //NSAutoreleasePool *pool = [NSAutoreleasePool new];
61 //dumpinfo("/System/Library/Frameworks/AppKit.framework/AppKit");
63 printf("Usage: gcinfo [-v] [-r] [--] library_or_executable_image [image2 ...]\n");
64 printf(" prints Garbage Collection readiness of named images, ignoring those without ObjC segments\n");
65 printf(" 'GC' - compiled with write-barriers, presumably otherwise aware\n");
66 printf(" 'RR' - retain/release (presumed) aware for non-GC\n");
67 printf(" 'GC-only' - compiled with write-barriers and marked at compile time as not being retain/release savvy\n");
68 printf(" -v - provide archtectural details\n");
69 printf(" -r - only show libraries that are non-GC, e.g. RR only\n");
70 printf(" -- - read files & directories from stdin, e.g. find /Plug-ins | gcinfo --\n");
71 printf("\nAuthor: blaine@apple.com\n");
74 for (i
= 1; i
< argc
; ++i
) {
75 if (!strcmp(argv
[i
], "-v")) {
79 if (!strcmp(argv
[i
], "-d")) {
83 if (!strcmp(argv
[i
], "-q")) {
87 if (!strcmp(argv
[i
], "-r")) {
92 if (!strcmp(argv
[i
], "-p")) {
96 if (!strcmp(argv
[i
], "--")) {
98 while (fgets(buf
, 1024, stdin
)) {
99 int len
= strlen(buf
);
115 void patchFile(uint32_t value
, size_t offset
) {
116 int fd
= open(FileName
, 1);
117 off_t lresult
= lseek(fd
, offset
, SEEK_SET
);
119 printf("couldn't seek to %x position on fd %d\n", offset
, fd
);
123 int wresult
= write(fd
, &value
, 4);
126 printf("didn't write new value\n");
129 printf("patched %s at offset %p\n", FileName
, offset
);
134 uint32_t iiflags(struct imageInfo
*ii
, uint32_t size
, bool needsFlip
) {
136 ii
->flags
= OSSwapInt32(ii
->flags
);
138 if (debug
) printf("flags->%x, nitems %d\n", ii
->flags
, size
/sizeof(struct imageInfo
));
139 uint32_t flags
= ii
->flags
;
140 if (patch
&& (flags
&0x2)==0) {
141 //printf("will patch %s at offset %p\n", FileName, (char*)(&ii->flags) - FileBase);
142 uint32_t newvalue
= flags
| 0x2;
143 if (needsFlip
) newvalue
= OSSwapInt32(newvalue
);
144 patchFile(newvalue
, (char*)(&ii
->flags
) - FileBase
);
146 for(int niis
= 1; niis
< size
/sizeof(struct imageInfo
); ++niis
) {
147 if (needsFlip
) ii
[niis
].flags
= OSSwapInt32(ii
[niis
].flags
);
148 if (ii
[niis
].flags
!= flags
) {
150 printf("XXX ii[%d].flags %x != ii[0].flags %x\n", niis
, ii
[niis
].flags
, flags
);
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");
165 void doimageinfo(struct imageInfo *ii, uint32_t size, bool needsFlip) {
166 uint32_t flags = iiflags(ii, size, needsFlip);
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;
179 sect
->offset
= OSSwapInt32(sect
->offset
);
180 sect
->size
= OSSwapInt32(sect
->size
);
182 // these guys aren't inline - they point elsewhere
183 gcip
->flags
= iiflags(start
+ sect
->offset
, sect
->size
, needsFlip
);
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;
193 sect
->offset
= OSSwapInt32(sect
->offset
);
194 sect
->size
= OSSwapInt64(sect
->size
);
196 // these guys aren't inline - they point elsewhere
197 gcip
->flags
= iiflags(start
+ sect
->offset
, sect
->size
, needsFlip
);
200 void doseg32(void *start
, struct segment_command
*seg
, bool needsFlip
, struct gcinfo
*gcip
) {
203 seg
->fileoff
= OSSwapInt32(seg
->fileoff
);
204 seg
->nsects
= OSSwapInt32(seg
->nsects
);
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;
211 struct section
*sect
= (struct section
*)(seg
+ 1);
212 for (int nsects
= 0; nsects
< seg
->nsects
; ++nsects
) {
213 // sections directly follow
215 dosect32(start
, sect
+ nsects
, needsFlip
, gcip
);
218 void doseg64(void *start
, struct segment_command_64
*seg
, bool needsFlip
, struct gcinfo
*gcip
) {
219 if (debug
) printf("segment name: %s\n", seg
->segname
);
220 if (seg
->segname
[0] && strcmp("__OBJC", seg
->segname
) && strcmp("__DATA", seg
->segname
)) return;
221 gcip
->hasObjC
= true;
224 seg
->fileoff
= OSSwapInt64(seg
->fileoff
);
225 seg
->nsects
= OSSwapInt32(seg
->nsects
);
228 struct section_64
*sect
= (struct section_64
*)(seg
+ 1);
229 for (int nsects
= 0; nsects
< seg
->nsects
; ++nsects
) {
230 // sections directly follow
232 dosect64(start
, sect
+ nsects
, needsFlip
, gcip
);
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.
246 uint32_t offset
; /* offset to the string */
248 char *ptr
; /* pointer to the string */
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*/
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
.
265 struct dylib_command
{
266 uint32_t cmd
; /* LC_ID_DYLIB, LC_LOAD_{,WEAK_}DYLIB,
268 uint32_t cmdsize
; /* includes pathname string */
269 struct dylib dylib
; /* the library identification */
273 void dodylib(void *start
, struct dylib_command
*dylibCmd
, bool needsFlip
) {
274 if (!verbose
) return;
277 int 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", ((void *)dylibCmd
)+dylibCmd
->dylib
.name
.offset
);
284 struct load_command
*doloadcommand(void *start
, struct load_command
*lc
, bool needsFlip
, bool is32
, struct gcinfo
*gcip
) {
286 lc
->cmd
= OSSwapInt32(lc
->cmd
);
287 lc
->cmdsize
= OSSwapInt32(lc
->cmdsize
);
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
);
297 if (debug
) printf("...segment32\n");
298 doseg32(start
, (struct segment_command
*)lc
, needsFlip
, gcip
);
300 case LC_SYMTAB
: if (debug
) printf("...dynamic symtab\n"); break;
301 case LC_DYSYMTAB
: if (debug
) printf("...symtab\n"); break;
303 dodylib(start
, (struct dylib_command
*)lc
, needsFlip
);
305 case LC_SUB_UMBRELLA
: if (debug
) printf("...load subumbrella\n"); break;
306 default: if (debug
) printf("cmd is %x\n", lc
->cmd
); break;
309 return (struct load_command
*)((void *)lc
+ lc
->cmdsize
);
312 void doofile(void *start
, uint32_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
);
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));
337 for (ncmds
= 0; ncmds
< mh
->ncmds
; ++ncmds
) {
338 lc
= doloadcommand(start
, lc
, isFlipped
, is32
, gcip
);
344 bzero((void *)GCInfo
, sizeof(GCInfo
));
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
;
352 for (int i
= 1; i
< 4 && GCInfo
[i
].arch
; ++i
) {
353 if (flags
!= GCInfo
[i
].flags
) {
358 if (allSame
&& (flags
& 0x2))
360 printf("*** not all GC in %s:\n", filename
);
362 if (allSame
&& !verbose
) {
363 printf("%s:", filename
);
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
);
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
);
385 if (debug
) printf("%d architectures\n", fh
->nfat_arch
);
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
);
391 arch_ptr
->offset
= OSSwapInt32(arch_ptr
->offset
);
392 arch_ptr
->size
= OSSwapInt32(arch_ptr
->size
);
394 doofile(start
+arch_ptr
->offset
, arch_ptr
->size
, &GCInfo
[narchs
]);
399 bool openFile(const char *filename
) {
403 int fd
= open(filename
, 0);
405 printf("couldn't open %s for reading\n", filename
);
408 int osresult
= fstat(fd
, &statb
);
410 printf("couldn't get size of %s\n", filename
);
414 FileSize
= statb
.st_size
;
415 FileBase
= malloc(FileSize
);
417 printf("couldn't malloc %d bytes\n", FileSize
);
421 off_t readsize
= read(fd
, FileBase
, FileSize
);
422 if (readsize
!= FileSize
) {
423 printf("read %d bytes, wanted %d\n", readsize
, FileSize
);
435 void dumpinfo(char *filename
) {
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);
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);
448 printf("don't understand %s!\n", filename
);