How to programmatically determine Cocoa's garbage collection settings for a plugin?

On Mac OS X using Objective-C 2, plug-in packages can be compiled with one of three garbage collection settings:

  • Not supported
  • Supported ( -fobjc-gc )
  • Required ( -fobjc-gc-only )

How to programmatically request a compiled set of plugins to determine which of these three settings was used?

+6
garbage-collection objective-c cocoa macos
source share
3 answers

In response to diciu, you can use the Mach-O API . You should parse the segments contained in the binary and look for __ OBJC ; The segment_command structure allows access to the segment flags.

You can also see the ClassDump project . It has a fairly complete Mach-O parser.

+3
source share

This is part of the __OBJC segment, but I do not know any APIs that expose it.

Garbage Collection:

 cristi:tmp diciu$ otool -v -o ./a.out ./a.out: Contents of (__DATA,__objc_classrefs) section 00000001000010b0 0x0 Contents of (__DATA,__objc_imageinfo) section version 0 flags 0x6 OBJC_IMAGE_SUPPORTS_GC 

No garbage collected:

 cristi:tmp diciu$ otool -v -o ./a.out ./a.out: Contents of (__DATA,__objc_classrefs) section 00000001000010b0 0x0 Contents of (__DATA,__objc_imageinfo) section version 0 flags 0x0 

The runtime does this using private functions: see gc_enforcer and uses * _objcInfoRequiresGC *

+4
source share

Probably the easiest way is to simply try to download the package using the NSBundle -loadAndReturnError: method. If the package does not load due to the fact that its GC settings are different from your application settings, you will receive an NSExecutableRuntimeMismatchError .

+1
source share

All Articles