Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Finding the architectures of a binary




On Nov 10, 2008, at 10:58 AM, Brian Bergstrand wrote:


On Nov 10, 2008, at 11:30 AM, Iceberg-Dev wrote:

I'm trying to find all the architectures available in a binary using code.

...

What I don't know/understand:

- I'm not sure to understand how you can inspect a binary without loading it in memory. It looks as if the dyld API work only with the running binary (no parameter for instance to enumerate the number of images with _dyld_image_count();).

- There doesn't seem to be an API for peff binaries. Is it safe to assume that the magic cookie is always at the beginning of the file?



Open the file and parse the info yourself using mach-o/loader.h and mach-o/fat.h. PEF is very rare these days, but you can do the same thing using CarbonCore/PEFBinaryFormat.h. Of course on OS X, PEF will only contain PPC code.

Thanks. I followed your suggestion.

Here's the code I come up with:

#import "ICArchitectureUtilities.h"

#include <mach-o/loader.h>
#include <mach-o/fat.h>

#include <CoreServices/CoreServices.h>

@implementation ICArchitectureUtilities

+ (NSArray *) architecturesOfFile:(NSString *) inPath
{
	if (inPath!=nil)
	{
		FILE * tFile;
	
		tFile=fopen([inPath fileSystemRepresentation],"r");
		
		if (tFile!=NULL)
		{
			uint32_t tMagicCookie;
			size_t tSize;
			
			tSize=sizeof(uint32_t);
			
			if (fread(&tMagicCookie,tSize,1,tFile)==1)
			{
				
#if BYTE_ORDER==LITTLE_ENDIAN
				tMagicCookie=CFSwapInt32(tMagicCookie);
#endif
				
				fseek(tFile,0,SEEK_SET);
				
				// PEF
					
				if (tMagicCookie==kPEFTag1)
				{
					PEFContainerHeader tContainerHeader;
					
					tSize=sizeof(PEFContainerHeader);
					
					if (fread(&tContainerHeader,tSize,1,tFile)==1)
					{

#if BYTE_ORDER==LITTLE_ENDIAN
tContainerHeader.tag2=CFSwapInt32(tContainerHeader.tag2);
tContainerHeader.architecture=CFSwapInt32 (tContainerHeader.architecture);
#endif


						if (tContainerHeader.tag2==kPEFTag2 &&
							tContainerHeader.architecture==kPowerPCCFragArch)
						{
							return [NSArray arrayWithObject:@"ppc"];
						}
					}
				}
				else
				{
					// mach-o
					
					if (tMagicCookie==FAT_MAGIC)
					{
						struct fat_header tFatHeader;
						
						tSize=sizeof(struct fat_header);
					
						if (fread(&tFatHeader,tSize,1,tFile)==1)
						{
							NSMutableArray * tMutableArray;
							
							tMutableArray=[NSMutableArray array];
							
							if (tMutableArray!=nil)
							{
								uint32_t i;
							
#if BYTE_ORDER==LITTLE_ENDIAN
								tFatHeader.nfat_arch=CFSwapInt32(tFatHeader.nfat_arch);
#endif

								for(i=0;i<tFatHeader.nfat_arch;i++)
								{
									struct fat_arch tFatArch;
								
									tSize=sizeof(struct fat_arch);
					
									if (fread(&tFatArch,tSize,1,tFile)==1)
									{
#if BYTE_ORDER==LITTLE_ENDIAN
										tFatArch.cputype=CFSwapInt32(tFatArch.cputype);
#endif										
									
										switch(tFatArch.cputype)
										{
											case CPU_TYPE_X86:
											
												[tMutableArray addObject:@"i386"];
												
												break;
												
											case CPU_TYPE_X86_64:
											
												[tMutableArray addObject:@"x86_64"];
												
												break;
												
											case CPU_TYPE_POWERPC:
											
												[tMutableArray addObject:@"ppc"];
												
												break;
												
											case CPU_TYPE_POWERPC64:
											
												[tMutableArray addObject:@"ppc64"];
												
												break;
										}
									
									}
								}
								
								[tMutableArray sortUsingSelector:@selector(compare:)];
								
								return tMutableArray;
							}
						}
					}
					else if (tMagicCookie==MH_MAGIC)
					{
						struct mach_header tMachHeader;
						
						tSize=sizeof(struct mach_header);
					
						if (fread(&tMachHeader,tSize,1,tFile)==1)
						{
#if BYTE_ORDER==LITTLE_ENDIAN
							tMachHeader.cputype=CFSwapInt32(tMachHeader.cputype);
#endif

							switch(tMachHeader.cputype)
							{
								case CPU_TYPE_X86:
								
									return [NSArray arrayWithObject:@"i386"];
									
								case CPU_TYPE_POWERPC:
								
									return [NSArray arrayWithObject:@"ppc"];
							}
						}
					}
					else if (tMagicCookie==MH_MAGIC_64)
					{
						struct mach_header_64 tMachHeader64;
						
						tSize=sizeof(struct mach_header_64);
					
						if (fread(&tMachHeader64,tSize,1,tFile)==1)
						{
#if BYTE_ORDER==LITTLE_ENDIAN
							tMachHeader64.cputype=CFSwapInt32(tMachHeader64.cputype);
#endif

							switch(tMachHeader64.cputype)
							{
								case CPU_TYPE_X86_64:
								
									return [NSArray arrayWithObject:@"x86_64"];
									
								case CPU_TYPE_POWERPC64:
								
									return [NSArray arrayWithObject:@"ppc64"];
							}
						}
					}
				}
			}
		}
	}
	
	return nil;
}

@end

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden
References: 
 >Finding the architectures of a binary (From: Iceberg-Dev <email@hidden>)
 >Re: Finding the architectures of a binary (From: Brian Bergstrand <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.