Codesign returned 1 (object ifile format invalid or unsuitable) bug

I’m working with Xcode 4.1 build 4B110f trying to get my iOS app ready for upload. It passes the Product|Archive step with no errors, asking twice for permission to sign something. But when I try a validate of the archive from the Organizer, it fails:

### Codesigning '/Users/uqrchern/Library/MobileDevice/Provisioning Profiles/70D2381D-3733-4F5D-88B2-4729572C2864.mobileprovision' with 'iPhone Distribution: Ron Chernich'
+ /usr/bin/codesign --force --preserve-metadata --sign iPhone Distribution: Ron Chernich --resource-rules=/var/folders/ul/ula1AHKnGpqQ9ftDnUL-l++++TM/-Tmp-/rybczU3EBd/Payload/ABRA-D.app/ResourceRules.plist --entitlements /var/folders/ul/ula1AHKnGpqQ9ftDnUL-l++++TM/-Tmp-/rybczU3EBd/entitlements_plistrZ1Vwko6 /var/folders/ul/ula1AHKnGpqQ9ftDnUL-l++++TM/-Tmp-/rybczU3EBd/Payload/ABRA-D.app
Program /usr/bin/codesign returned 1 : [/var/folders/ul/ula1AHKnGpqQ9ftDnUL-l++++TM/-Tmp-/rybczU3EBd/Payload/ABRA-D.app: replacing existing signature
/var/folders/ul/ula1AHKnGpqQ9ftDnUL-l++++TM/-Tmp-/rybczU3EBd/Payload/ABRA-D.app: object file format invalid or unsuitable
]
error: codesign failed with error 1

I’ve looked at all the similar problems and solutions (some of which make no sense whatever, or apply to really old versions of the tools). None have made the slightest difference.

  • didSelectRowAtIndexPath not working, Swift 3
  • AFNetworking 2.0 - use responseObject as NSDictionary
  • Swift 2 JSON POST request
  • Local storage for iPhone app
  • Per configuration app group entitlement strings in Xcode?
  • Storyboard segues causing memory leaks
  • I’ve also checked 3 times that verify is using the “production” certificate, as is the codesign step that produces the archive. I’ve even turned the above output into a schell script so I could try all certificates manually: same result every time.

    Maybe the .app file being signed really is unsuitable?

    Incidentally, codesign has no version flag, but the man page is dated June 1 2006. The binary has a file date of Nov 20 2010.

    UPDATE (next day):

    Researching the problem further found an obscure reference saying that codesign needs the following environment var set:

    CODESIGN_ALLOCATE=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate
    

    Using the output from a failed Validate run, I created a shell script which exported this var just before the failing codesign –force step and viola! The codesign works.

    But this does not really help me prepare my code for upload. Is there a way to include this into the script run by the Organizer Validate button??

    A LITTLE LATER STILL, THE SOLUTION! :

    Under the theory there is a script someplace which generates all the commands run during an Organizer Validate… run, I did some digging with grep and find. The script indeed exists and it’s name is:

    /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/PackageApplication
    

    It’s just Perl and the fix is to add the required environment var to the associative array %ENV right at the start, say at line 72:

    $ENV{CODESIGN_ALLOCATE} = '/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate';
    

    This totally fixes the problem. I’ve no idea where all the other posters on the web who think they fixed it by combinations of deleting certificates, building clean, shutting down and restarting Xcode, etc, etc are coming from. I’ll just quietly assert that this fix favors science over superstition and works for me under Xcode 4.1 Build 4B110f and its associated PackageApplication script, running under Snow Leopard 10.6.8 with Perl 5.10.0

    3 Solutions Collect From Internet About “Codesign returned 1 (object ifile format invalid or unsuitable) bug”

    Just so that this can be taken off the unanswered list. Like you said, you need to add CODESIGN_ALLOCATE to the $ENV array:

    $ENV{CODESIGN_ALLOCATE} = '/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate';
    

    If everyone is in agreement here, I think this question can finally be closed.

    When using a more recent version of Xcode, the default location is:

    $ENV{CODESIGN_ALLOCATE} = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate';
    

    In case you get this on a recent version of Xcode, what you actually want is, in the shell:

    export CODESIGN_ALLOCATE=`xcode-select -print-path`/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate
    

    which will use the codesign_allocate from the version of Xcode you are using.

    You can update the version of Xcode the command line tools use by running xcode-select -switch

    I had this workaround in place for a long time, but after upgrading to Xcode 4.3 with iOS 5.1 SDK, my signing script (which calls codesign) stopped working with a cannot find code object on disk error:

    output/Enterprise/Payload/MyProduct.app/MyProduct: replacing invalid existing signature    
    output/Enterprise/Payload/MyProduct.app/MyProduct: cannot find code object on disk     
    Code signing failed, not creating .ipa file    
    

    It seems this workaround isn’t necessary for binaries built with Xcode 4.3. To fix, I updated my bash script to check if the location exists before exporting it:

      # Only export the environment variable if the location exists,
      # otherwise it breaks the signing process!
      if [ -f "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate" ]
      then
        echo Export environment variable for codesign_allocate location
        export CODESIGN_ALLOCATE=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate
      fi