-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
5,426 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright 2011 Shinichiro Hamaji. All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions | ||
// are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright | ||
// notice, this list of conditions and the following disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above | ||
// copyright notice, this list of conditions and the following | ||
// disclaimer in the documentation and/or other materials | ||
// provided with the distribution. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY Shinichiro Hamaji ``AS IS'' AND ANY | ||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Shinichiro Hamaji OR | ||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
// SUCH DAMAGE. | ||
|
||
// A command line tool to extract a Mach-O binary from a fat binary. | ||
|
||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/mman.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <unistd.h> | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
#include "mach-o/fat.h" | ||
|
||
using namespace std; | ||
|
||
bool be = false; | ||
|
||
static void fixEndian(uint32_t* p) { | ||
if (!be) { | ||
return; | ||
} | ||
|
||
uint32_t v = *p; | ||
*p = (v << 24) | ((v << 8) & 0x00ff0000) | ((v >> 8) & 0xff00) | (v >> 24); | ||
} | ||
|
||
static const char* getArchName(uint32_t a) { | ||
switch (a) { | ||
case CPU_TYPE_X86: | ||
return "x86"; | ||
case CPU_TYPE_X86_64: | ||
return "x86-64"; | ||
case CPU_TYPE_POWERPC: | ||
return "ppc"; | ||
case CPU_TYPE_POWERPC64: | ||
return "ppc64"; | ||
default: | ||
return "???"; | ||
} | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
if (argc < 2) { | ||
fprintf(stderr, "Usage: %s fat [arch out]\n", argv[0]); | ||
exit(1); | ||
} | ||
|
||
int fd = open(argv[1], O_RDONLY); | ||
if (fd < 0) { | ||
perror("open"); | ||
exit(1); | ||
} | ||
|
||
off_t len = lseek(fd, 0, SEEK_END); | ||
char* bin = reinterpret_cast<char*>( | ||
mmap(NULL, len, | ||
PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0)); | ||
|
||
fat_header* header = reinterpret_cast<fat_header*>(bin); | ||
|
||
if (header->magic == FAT_CIGAM) { | ||
be = true; | ||
} else if (header->magic != FAT_MAGIC) { | ||
fprintf(stderr, "Not fat\n"); | ||
exit(1); | ||
} | ||
|
||
fixEndian(&header->nfat_arch); | ||
|
||
printf("magic=%x nfat_arch=%d\n", | ||
header->magic, header->nfat_arch); | ||
|
||
map<string, fat_arch*> archs; | ||
|
||
char* fat_ptr = bin + sizeof(fat_header); | ||
for (uint32_t i = 0; i < header->nfat_arch; i++) { | ||
fat_arch* arch = reinterpret_cast<fat_arch*>(fat_ptr); | ||
|
||
fixEndian(&arch->cputype); | ||
fixEndian(&arch->cpusubtype); | ||
fixEndian(&arch->offset); | ||
fixEndian(&arch->size); | ||
fixEndian(&arch->align); | ||
|
||
const char* name = getArchName(arch->cputype); | ||
|
||
printf("cputype=%d (%s) cpusubtype=%d offset=%d size=%d align=%d\n", | ||
arch->cputype, name, arch->cpusubtype, | ||
arch->offset, arch->size, arch->align); | ||
|
||
archs.insert(make_pair(name, arch)); | ||
|
||
fat_ptr += sizeof(fat_arch); | ||
} | ||
|
||
for (int i = 2; i + 1 < argc; i += 2) { | ||
const char* arch_name = argv[i]; | ||
map<string, fat_arch*>::const_iterator found = archs.find(arch_name); | ||
if (found == archs.end()) { | ||
printf("unknown arch: %s\n", arch_name); | ||
continue; | ||
} | ||
|
||
fat_arch* arch = found->second; | ||
FILE* fp = fopen(argv[i+1], "wb"); | ||
fwrite(bin + arch->offset, 1, arch->size, fp); | ||
fclose(fp); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) 2004, 2008, 2009 Apple Inc. All rights reserved. | ||
* | ||
* @APPLE_LICENSE_HEADER_START@ | ||
* | ||
* This file contains Original Code and/or Modifications of Original Code | ||
* as defined in and that are subject to the Apple Public Source License | ||
* Version 2.0 (the 'License'). You may not use this file except in | ||
* compliance with the License. Please obtain a copy of the License at | ||
* http://www.opensource.apple.com/apsl/ and read it before using this | ||
* file. | ||
* | ||
* The Original Code and all software distributed under the License are | ||
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | ||
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | ||
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | ||
* Please see the License for the specific language governing rights and | ||
* limitations under the License. | ||
* | ||
* @APPLE_LICENSE_HEADER_END@ | ||
*/ | ||
|
||
#ifndef __TYPES_H_ | ||
#define __TYPES_H_ | ||
|
||
#include <stdint.h> | ||
//#include <sys/_types.h> | ||
|
||
#if __GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7 | ||
#define __strfmonlike(fmtarg, firstvararg) \ | ||
__attribute__((__format__ (__strfmon__, fmtarg, firstvararg))) | ||
#define __strftimelike(fmtarg) \ | ||
__attribute__((__format__ (__strftime__, fmtarg, 0))) | ||
#else | ||
#define __strfmonlike(fmtarg, firstvararg) | ||
#define __strftimelike(fmtarg) | ||
#endif | ||
|
||
typedef int __darwin_nl_item; | ||
typedef int __darwin_wctrans_t; | ||
#ifdef __LP64__ | ||
typedef uint32_t __darwin_wctype_t; | ||
#else /* !__LP64__ */ | ||
typedef unsigned long __darwin_wctype_t; | ||
#endif /* __LP64__ */ | ||
|
||
#ifdef __WCHAR_MAX__ | ||
#define __DARWIN_WCHAR_MAX __WCHAR_MAX__ | ||
#else /* ! __WCHAR_MAX__ */ | ||
#define __DARWIN_WCHAR_MAX 0x7fffffff | ||
#endif /* __WCHAR_MAX__ */ | ||
|
||
#if __DARWIN_WCHAR_MAX > 0xffffU | ||
#define __DARWIN_WCHAR_MIN (-0x7fffffff - 1) | ||
#else | ||
#define __DARWIN_WCHAR_MIN 0 | ||
#endif | ||
#define __DARWIN_WEOF ((__darwin_wint_t)-1) | ||
|
||
#ifndef _FORTIFY_SOURCE | ||
# if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && ((__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__-0) < 1050) | ||
# define _FORTIFY_SOURCE 0 | ||
# else | ||
# define _FORTIFY_SOURCE 2 /* on by default */ | ||
# endif | ||
#endif | ||
|
||
#endif /* __TYPES_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (c) 2000, 2005, 2008 Apple Inc. All rights reserved. | ||
* | ||
* @APPLE_LICENSE_HEADER_START@ | ||
* | ||
* This file contains Original Code and/or Modifications of Original Code | ||
* as defined in and that are subject to the Apple Public Source License | ||
* Version 2.0 (the 'License'). You may not use this file except in | ||
* compliance with the License. Please obtain a copy of the License at | ||
* http://www.opensource.apple.com/apsl/ and read it before using this | ||
* file. | ||
* | ||
* The Original Code and all software distributed under the License are | ||
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | ||
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | ||
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | ||
* Please see the License for the specific language governing rights and | ||
* limitations under the License. | ||
* | ||
* @APPLE_LICENSE_HEADER_END@ | ||
*/ | ||
/* | ||
* Copyright (c) 1989, 1993 | ||
* The Regents of the University of California. All rights reserved. | ||
* (c) UNIX System Laboratories, Inc. | ||
* All or some portions of this file are derived from material licensed | ||
* to the University of California by American Telephone and Telegraph | ||
* Co. or Unix System Laboratories, Inc. and are reproduced herein with | ||
* the permission of UNIX System Laboratories, Inc. | ||
* | ||
* This code is derived from software contributed to Berkeley by | ||
* Paul Borman at Krystal Technologies. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* 3. All advertising materials mentioning features or use of this software | ||
* must display the following acknowledgement: | ||
* This product includes software developed by the University of | ||
* California, Berkeley and its contributors. | ||
* 4. Neither the name of the University nor the names of its contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
* SUCH DAMAGE. | ||
* | ||
* @(#)ctype.h 8.4 (Berkeley) 1/21/94 | ||
*/ | ||
|
||
#ifndef MAC_CTYPE_H_ | ||
#define MAC_CTYPE_H_ | ||
|
||
#define _CTYPE_A 0x00000100L /* Alpha */ | ||
#define _CTYPE_C 0x00000200L /* Control */ | ||
#define _CTYPE_D 0x00000400L /* Digit */ | ||
#define _CTYPE_G 0x00000800L /* Graph */ | ||
#define _CTYPE_L 0x00001000L /* Lower */ | ||
#define _CTYPE_P 0x00002000L /* Punct */ | ||
#define _CTYPE_S 0x00004000L /* Space */ | ||
#define _CTYPE_U 0x00008000L /* Upper */ | ||
#define _CTYPE_X 0x00010000L /* X digit */ | ||
#define _CTYPE_B 0x00020000L /* Blank */ | ||
#define _CTYPE_R 0x00040000L /* Print */ | ||
#define _CTYPE_I 0x00080000L /* Ideogram */ | ||
#define _CTYPE_T 0x00100000L /* Special */ | ||
#define _CTYPE_Q 0x00200000L /* Phonogram */ | ||
|
||
/* | ||
* Backward compatibility | ||
*/ | ||
#define _A _CTYPE_A /* Alpha */ | ||
#define _C _CTYPE_C /* Control */ | ||
#define _D _CTYPE_D /* Digit */ | ||
#define _G _CTYPE_G /* Graph */ | ||
#define _L _CTYPE_L /* Lower */ | ||
#define _P _CTYPE_P /* Punct */ | ||
#define _S _CTYPE_S /* Space */ | ||
#define _U _CTYPE_U /* Upper */ | ||
#define _X _CTYPE_X /* X digit */ | ||
#define _B _CTYPE_B /* Blank */ | ||
#define _R _CTYPE_R /* Print */ | ||
#define _I _CTYPE_I /* Ideogram */ | ||
#define _T _CTYPE_T /* Special */ | ||
#define _Q _CTYPE_Q /* Phonogram */ | ||
|
||
#endif /* !MAC_CTYPE_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | ||
* | ||
* @APPLE_LICENSE_HEADER_START@ | ||
* | ||
* This file contains Original Code and/or Modifications of Original Code | ||
* as defined in and that are subject to the Apple Public Source License | ||
* Version 2.0 (the 'License'). You may not use this file except in | ||
* compliance with the License. Please obtain a copy of the License at | ||
* http://www.opensource.apple.com/apsl/ and read it before using this | ||
* file. | ||
* | ||
* The Original Code and all software distributed under the License are | ||
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | ||
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | ||
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | ||
* Please see the License for the specific language governing rights and | ||
* limitations under the License. | ||
* | ||
* @APPLE_LICENSE_HEADER_END@ | ||
*/ | ||
#ifndef _MACH_O_FAT_H_ | ||
#define _MACH_O_FAT_H_ | ||
/* | ||
* This header file describes the structures of the file format for "fat" | ||
* architecture specific file (wrapper design). At the begining of the file | ||
* there is one fat_header structure followed by a number of fat_arch | ||
* structures. For each architecture in the file, specified by a pair of | ||
* cputype and cpusubtype, the fat_header describes the file offset, file | ||
* size and alignment in the file of the architecture specific member. | ||
* The padded bytes in the file to place each member on it's specific alignment | ||
* are defined to be read as zeros and can be left as "holes" if the file system | ||
* can support them as long as they read as zeros. | ||
* | ||
* All structures defined here are always written and read to/from disk | ||
* in big-endian order. | ||
*/ | ||
|
||
/* | ||
* <mach/machine.h> is needed here for the cpu_type_t and cpu_subtype_t types | ||
* and contains the constants for the possible values of these types. | ||
*/ | ||
#include <stdint.h> | ||
#include <mach/machine.h> | ||
//#include <architecture/byte_order.h> | ||
|
||
#define FAT_MAGIC 0xcafebabe | ||
#define FAT_CIGAM 0xbebafeca /* NXSwapLong(FAT_MAGIC) */ | ||
|
||
struct fat_header { | ||
uint32_t magic; /* FAT_MAGIC */ | ||
uint32_t nfat_arch; /* number of structs that follow */ | ||
}; | ||
|
||
struct fat_arch { | ||
cpu_type_t cputype; /* cpu specifier (int) */ | ||
cpu_subtype_t cpusubtype; /* machine specifier (int) */ | ||
uint32_t offset; /* file offset to this object file */ | ||
uint32_t size; /* size of this object file */ | ||
uint32_t align; /* alignment as a power of 2 */ | ||
}; | ||
|
||
#endif /* _MACH_O_FAT_H_ */ |
Oops, something went wrong.