On 01/09/2011, at 20:17, cocoa-dev-requ...@lists.apple.com wrote:
> From: Martin Wierschin <mar...@nisus.com>
> Date: 1 de setembro de 2011 19:11:24 BRT
> To: Dave DeLong <davedel...@me.com>
> Cc: Cocoa Dev List <cocoa-dev@lists.apple.com>
> 
> Offhand does anyone know how to inspect the architecture(s) of a plain 
> executable file? I've been googling for a little bit and haven't hit upon 
> anything that works yet.

Sure, that's easy to do. Define a C function like this:

#include <mach-o/fat.h>
char* FindMachoHeader(char* input, cpu_type_t cpu) {
        struct fat_header* header = (struct fat_header*)input;
        if (OSSwapBigToHostInt32(header->magic)==FAT_MAGIC) {
                struct fat_arch* arch = (struct fat_arch*)(input+sizeof(struct 
fat_header));
                for (NSUInteger 
i=0;i<OSSwapBigToHostInt32(header->nfat_arch);i++) {
                        if (OSSwapBigToHostInt32(arch[i].cputype)==cpu) {
                                return 
input+OSSwapBigToHostInt32(arch[i].offset);
                        }                                                       
                                        
                }
        } else if (OSSwapBigToHostInt32(((struct 
fat_arch*)input)->cputype)==cpu) {
                return input;
        }
        return NULL;
}

Then get the executable file with something like:
        NSData* executable = [NSData 
dataWithContentsOfMappedFile:pathToExecutableFile];
and call the function like
        if (FindMachoHeader([executable bytes],CPU_TYPE_X86)) { // or 
CPU_TYPE_POWERPC if applicable
                // if it returns non-null that architecture is present
        }

(Disclaimer: partially written in Mail, proper consistency checking and so 
forth must be added)

HTH,
--
Rainer Brockerhoff  <rai...@brockerhoff.net>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
Weblog: http://www.brockerhoff.net/blog

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to