Sunday, February 24, 2013

Illegal instruction: 4

Some old arm v6 binaries that compiled using iPhone-gcc and old sdk have "Illegal instruction: 4" when using devices with A6/A6X CPU such as iPhone 5 / iPad 4 as discussed in http://code.google.com/p/iphone-gcc-full/issues/detail?id=6
The is how to patch these binaries without recompiling

perl -pe 's/\x{00}\x{30}\x{93}\x{e4}/\x{00}\x{30}\x{93}\x{e5}/g;s/\x{00}\x{30}\x{d3}\x{e4}/\x{00}\x{30}\x{d3}\x{e5}/g;' < old_ios_binary > old_ios_binary_patched
chmod +x old_ios_binary_patched
ldid -s old_ios_binary_patched
mv old_ios_binary old_ios_binary_original
mv old_ios_binary_patched old_ios_binary


If you have gnu sed in iOS or OS X, you can patch directly without the temp file in one step
sed -i'' 's/\x00\x30\x93\xe4/\x00\x30\x93\xe5/g;s/\x00\x30\xd3\xe4/\x00\x30\xd3\xe5/g;' old_ios_binary
ldid -s old_ios_binary


iphone-gcc patched package for iPhone 5 / iPad 4 is here
http://code.google.com/p/apiexplorer/downloads/list

Monday, February 4, 2013

swizzleMethodsForClass

swizzleMethodsForClass.m Select all
#import <objc/runtime.h> // swap a class's instance method selectors, we do this to overload existing methods in category declarations void swizzleMethodsForClass(Class c, SEL origMethodSel, SEL newMethodSel) { NSLog(@"swizzling %@ instance methods: %@ -> %@", NSStringFromClass(c), NSStringFromSelector(origMethodSel), NSStringFromSelector(newMethodSel)); Method origMethod = class_getInstanceMethod(c, origMethodSel); Method newMethod = class_getInstanceMethod(c, newMethodSel); // check if method is inherited from superclass if(class_addMethod(c, origMethodSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) class_replaceMethod(c, newMethodSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); // exchange un-subclassed method else method_exchangeImplementations(origMethod, newMethod); } @interface UIDevice (SpoofUDID) @end #define UDID_TO_SPOOF @"e0101010d38bde8e6740011211af315301010223" @implementation UIDevice (SpoofUDID) // swizzle this instance method for UIDevice class - (NSString *) spoofUniqueIdentifier { static NSString *spoofUDID = UDID_TO_SPOOF; NSLog(@"spoofing %@ instead of %@", spoofUDID, [[UIDevice currentDevice] spoofUniqueIdentifier]); return spoofUDID; } @end // call this from your app delegate - (void) initUDID { NSString *UDID = [[UIDevice currentDevice] uniqueIdentifier]; NSLog(@"this is my old udid: %@", UDID); swizzleMethodsForClass([UIDevice class], @selector(uniqueIdentifier), @selector(spoofUniqueIdentifier)); NSString *UDID2 = [[UIDevice currentDevice] uniqueIdentifier]; NSLog(@"this is my new udid: %@", UDID2); }



Friday, September 21, 2012

architecture cputype (12) cpusubtype (11)

otool -h /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/usr/lib/crt1.o

Output is

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/usr/lib/crt1.o (architecture armv7):
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
 0xfeedface      12          9  0x00          1     3        500 0x00002000
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/usr/lib/crt1.o (architecture armv7f):
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
 0xfeedface      12         10  0x00          1     3        500 0x00002000
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/usr/lib/crt1.o (architecture armv7k):
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
 0xfeedface      12         12  0x00          1     3        500 0x00002000
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/usr/lib/crt1.o (architecture cputype (12) cpusubtype (11)):
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
 0xfeedface      12         11  0x00          1     3        500 0x00002000




grep CPU_SUBTYPE_ARM_V7 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/usr/include/mach/machine.h

Output is

#define CPU_SUBTYPE_ARM_V7  ((cpu_subtype_t) 9)
#define CPU_SUBTYPE_ARM_V7F  ((cpu_subtype_t) 10) /* Cortex A9 */
#define CPU_SUBTYPE_ARM_V7S  ((cpu_subtype_t) 11) /* Swift */
#define CPU_SUBTYPE_ARM_V7K  ((cpu_subtype_t) 12) /* Kirkwood40 */

Sunday, June 24, 2012

How to install thoes under Xcode 4.5 (iOS 6)

(1) Installation (note : you have to install Command Line Tools (Mountain Lion) for Xcode 4.5)
if you don't have Command Line Tools use
/Applications/Xcode.app/Contents/Developer/usr/bin/git
install_theos.sh    Select all
# clone theos.git cd ~ git clone git://github.com/DHowett/theos.git # clone iphoneheaders.git cd ~/theos/ mv include include.bak git clone git://github.com/rpetrich/iphoneheaders.git include for FILE in include.bak/*.h; do mv $FILE include/; done rmdir include.bak/ # get IOSurfaceAPI.h cd ~/theos/include/IOSurface/ curl -O -k https://raw.github.com/javacom/toolchain4/master/Projects/IOSurfaceAPI.h # clone theos-nic-templates.git cd ~/theos/templates/ git clone git://github.com/orikad/theos-nic-templates.git # get dpkg-deb for Mac OS X cd ~/theos curl -O http://test.saurik.com/francis/dpkg-deb-fat chmod a+x dpkg-deb-fat sudo mkdir -p /usr/local/bin sudo mv dpkg-deb-fat /usr/local/bin/dpkg-deb # get ldid for Mac OS X cd ~/theos/bin curl -O http://dl.dropbox.com/u/3157793/ldid chmod a+x ldid # get libsubstrate.dylib substrate.h cd ~/theos curl -OL http://apt.saurik.com/debs/mobilesubstrate_0.9.3366-1_iphoneos-arm.deb dpkg-deb -x mobilesubstrate_0.9.3366-1_iphoneos-arm.deb mobilesubstrate cp mobilesubstrate/Library/Frameworks/CydiaSubstrate.framework/CydiaSubstrate ~/theos/lib/libsubstrate.dylib cp mobilesubstrate/Library/Frameworks/CydiaSubstrate.framework/Headers/CydiaSubstrate.h include/substrate.h


Download and untar iPhoneOS5.1.sdk.tgz to Xcode
cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/
tar xzvf ~/Downloads/iPhoneOS5.1.sdk.tgz


(2) get preferenceloader.git for test build
mkdir -p ~/Projects
cd ~/Projects/
git clone git://github.com/DHowett/preferenceloader.git
cd ~/Projects/preferenceloader/



(3) Modify Makefile, change from

include framework/makefiles/common.mk

to

#export TARGET=iphone:latest:2.0
export ARCHS=armv7
include $(THEOS)/makefiles/common.mk
#include framework/makefiles/common.mk


(4) Test build preferenceloader
cd ~/Projects/preferenceloader/
export THEOS=~/theos
make
make package



(5.1) Clone IconRenamer Project
#get CaptainHook headers
cd ~/theos/include/
rm -fr CaptainHook
git clone git://github.com/rpetrich/CaptainHook.git
#get and build IconRenamer
cd ~/Projects
git clone git://github.com/rpetrich/IconRenamer.git
cd ~/Projects/IconRenamer
rmdir framework; ln -sf ~/theos framework

(5.2) Modify Makefile to

export TARGET=iphone:5.1
TWEAK_NAME = IconRenamer
IconRenamer_OBJC_FILES = IconRenamer.m
IconRenamer_FRAMEWORKS = Foundation UIKit

ADDITIONAL_CFLAGS = -std=c99

include $(THEOS)/makefiles/common.mk
include $(THEOS)/makefiles/tweak.mk

(5.3) Build IconRenamer

export THEOS=~/theos
cd ~/Projects/IconRenamer
make
make package


(6) Test build UICatalog
cd ~/Projects
svn co http://apiexplorer.googlecode.com/svn/trunk/UICatalog UICatalog
cd ~/Projects/UICatalog
sed '/^SYSROOT/d' Makefile.theos > Makefile
export THEOS=~/theos
make
make ipa
.
.
.

Monday, March 5, 2012

Xcode 4.3 command line compile

In case you did't install command line tools


/Applications/Xcode.app/Contents/Developer/usr/bin/gcc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk -framework Foundation helloworld.m -o helloworld



git command

/Applications/Xcode.app/Contents/Developer/usr/bin/git clone git://github.com/username/project.git


where is PackageMaker?

"Auxiliary tools for Xcode"

.
.
.

Monday, January 30, 2012

Darwin CC Tools odcctools_782-2_iphoneos-arm.deb

Darwin CC Tools, linker and assembler for the iOS.
v782 update
Supports armv6 and armv7

Download here http://apiexplorer.googlecode.com/files/odcctools_782-2_iphoneos-arm.deb
(odcctools_782-2 fixed the missing ld issue)
Instruction to build, see here http://github.com/javacom/toolchain4


The last build was v286 and released in 2008
.
.
.
gdb v1708 for iOS5+

Download here http://apiexplorer.googlecode.com/files/gdb_1708_iphoneos-arm.deb

.
.
.
vim with multi-byte support

Download here http://code.google.com/p/apiexplorer/downloads/detail?name=vim_7.1-4_iphoneos-arm.deb

.
.
.

Sunday, October 30, 2011

How to install perl, theos and iphone-gcc in iPhone

(1) Install the following packages in Cydia
APT 0.6 Transitional (and all its dependencies)
wget

(2) Use SSH login shell commands to install perl & theos (or install them in Cydia by adding Sources)

echo "deb http://coredev.nl/cydia iphone main" > /etc/apt/sources.list.d/coredev.nl.list
wget http://coredev.nl/cydia/coredev.pub
apt-key add coredev.pub
apt-get update
apt-get install perl
echo "deb http://nix.howett.net/theos ./" > /etc/apt/sources.list.d/howett.net.list
apt-get update
apt-get install net.howett.theos rsync


If you have No space left on device error when installing perl , relocate /usr/local to /var/stash/local

(3) Use SSH login shell commands to install iphone-gcc and ldid (or install them in Cydia)

wget http://apt.saurik.com/debs/libgcc_4.2-20080410-1-6_iphoneos-arm.deb
dpkg -i libgcc_4.2-20080410-1-6_iphoneos-arm.deb
apt-get update
apt-get install iphone-gcc
apt-get install make ldid zip unzip com.ericasadun.utilities


(4) Download SDK3.2 Headers and Libs from here
Please take note that the current iPhone-gcc does not support iOS SDK 4.0 or above

(5) copy iPhoneSDKHeadersAndLibs_32.pkg to iPhone

(6) Install SDK and additional libraries to sdk

apt-get update
apt-get install xar cpio
xar -xf iPhoneSDKHeadersAndLibs_32.pkg Payload
cat Payload | zcat | cpio -id
mv Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk /var/sdk
cd /var/sdk/usr/lib/
ln -s libstdc++.6.dylib libstdc++.dylib
ln -s crt1.o crt1.10.5.o
ln -s dylib1.o dylib1.10.5.o
cp -p /usr/lib/libgcc_s.10.5.dylib .



(7) Create a command line tool project

/var/theos/bin/nic.pl helloworld


(8) Choose [4.] iphone/tool

(9) Edit main.mm and add printf and cout like this
main.mm Select all

#include <iostream>
using namespace std;
int main(int argc, char **argv, char **envp) {
    printf("Hello World\n");
    cout << "Hello CPP" << endl;
    return 0;
}


(10) Add the ADDITIONAL_CFLAGS and ADDITIONAL_CPPFLAGS in Makefile like this
Makefile (Tool) Select all

export THEOS=/var/theos
include $(THEOS)/makefiles/common.mk

TOOL_NAME = helloworld
helloworld_FILES = main.mm

ADDITIONAL_CFLAGS = -I"$(SYSROOT)/usr/lib/gcc/arm-apple-darwin10/4.2.1/include"
ADDITIONAL_CPPFLAGS = -I"$(SYSROOT)/usr/include/c++/4.2.1"
ADDITIONAL_CPPFLAGS += -I"$(SYSROOT)/usr/include/c++/4.2.1/armv6-apple-darwin10"

include $(THEOS_MAKE_PATH)/tool.mk



(11) Make and test run

cd helloworld
make clean
make
./obj/helloworld


(12) For app, the sample Makefile is
Makefile (Application) Select all

export THEOS=/var/theos
include $(THEOS)/makefiles/common.mk

APPLICATION_NAME = myapp
myapp_FILES = main.m
myapp_FILES += $(wildcard Classes/*.m)

myapp_FRAMEWORKS = UIKit

PCH:=$(wildcard *.pch)

ADDITIONAL_CFLAGS = -I"$(SYSROOT)/usr/lib/gcc/arm-apple-darwin10/4.2.1/include"
ADDITIONAL_CPPFLAGS = -I"$(SYSROOT)/usr/include/c++/4.2.1"
ADDITIONAL_CPPFLAGS += -I"$(SYSROOT)/usr/include/c++/4.2.1/armv6-apple-darwin10"
ifneq ($(PCH),)
ADDITIONAL_CFLAGS += -include $(PCH)
ADDITIONAL_CPPFLAGS += -include $(PCH)
endif

include $(THEOS_MAKE_PATH)/application.mk

RESOURCESFOLDER=./Resources
PAYLOADFOLDER=$(THEOS_STAGING_DIR)
OBJFOLDER=./obj
INFOPLIST:=$(wildcard *Info.plist)
INFOPLIST+=$(wildcard Resources/*Info.plist)
PNGFILES:=$(wildcard Resources/*.png)
LPROJFILES:=$(wildcard Resources/*.lproj)
APPVERSION=$(shell plutil -key CFBundleVersion $(INFOPLIST) 2>&1)

# make ipa
# replace 4 spaces with TAB for below
ipa:    stage
    rm -fr $(PAYLOADFOLDER)/* $(APPLICATION_NAME)_$(APPVERSION).ipa
    rm -f Payload
    mkdir -p $(PAYLOADFOLDER)/$(APPLICATION_NAME).app
    cp $(OBJFOLDER)/$(APPLICATION_NAME) $(PAYLOADFOLDER)/$(APPLICATION_NAME).app/.
ifneq ($(PNGFILES),)
    cp -rp $(RESOURCESFOLDER)/*.png $(PAYLOADFOLDER)/$(APPLICATION_NAME).app/.
endif
ifneq ($(LPROJFILES),)
    cp -rp $(RESOURCESFOLDER)/*.lproj $(PAYLOADFOLDER)/$(APPLICATION_NAME).app/.
endif
    @echo "APPL????" > $(PAYLOADFOLDER)/$(APPLICATION_NAME).app/PkgInfo
    cp -p $(INFOPLIST) $(PAYLOADFOLDER)/$(APPLICATION_NAME).app/.
    ln -sf $(PAYLOADFOLDER) Payload
    zip -r $(APPLICATION_NAME)_$(APPVERSION).ipa Payload > /dev/null


(13) For mobilesubstrate extension, the sample Makefile is
Makefile (Tweak) Select all

export THEOS=/var/theos
include $(THEOS)/makefiles/common.mk

TWEAK_NAME = mytweak
APP_ID = com.mycompany.mytweak
mytweak_FILES = Tweak.xm
mytweak_FRAMEWORKS = UIKit

ADDITIONAL_CFLAGS = -I"$(SYSROOT)/usr/lib/gcc/arm-apple-darwin10/4.2.1/include"
ADDITIONAL_CPPFLAGS = -I"$(SYSROOT)/usr/include/c++/4.2.1"
ADDITIONAL_CPPFLAGS += -I"$(SYSROOT)/usr/include/c++/4.2.1/armv6-apple-darwin10"

include $(THEOS_MAKE_PATH)/tweak.mk

# make package
# replace 4 spaces with TAB for below
after-stage::
    find $(THEOS_STAGING_DIR) -iname '*.plist' -exec plutil -convert binary1 {} \;
    $(FAKEROOT) chown -R 0:80 $(THEOS_STAGING_DIR)

PACKAGEFOLDER=./layout
CONTROLFILE=control
PACKAGENAME=$(shell grep ^Package: $(CONTROLFILE) | cut -d ' ' -f 2)
PACKAGEVERSION=$(shell grep ^Version: $(CONTROLFILE) | cut -d ' ' -f 2)
ARCH=$(shell grep ^Architecture: $(CONTROLFILE) | cut -d ' ' -f 2)
OBJFOLDER=./obj
_EXCLUDES ?= tmp _MTN .git .svn .DS_Store ._*
_EXCLUDE_COMMANDLINE := $(foreach exclude,$(_EXCLUDES),--exclude "$(exclude)")


# with dpkg-deb
# replace 4 spaces with TAB for below
deb: stage
    rm -fr $(PACKAGENAME)_$(PACKAGEVERSION)_$(ARCH).deb $(PACKAGEFOLDER)/control.tar.gz $(PACKAGEFOLDER)/data.tar.gz $(PACKAGEFOLDER)/tmp
    mkdir -p $(PACKAGEFOLDER)/Library/MobileSubstrate/DynamicLibraries/
    mkdir -p $(PACKAGEFOLDER)/DEBIAN
    cp $(OBJFOLDER)/$(TWEAK_NAME).dylib $(PACKAGEFOLDER)/Library/MobileSubstrate/DynamicLibraries/.
    cp $(CONTROLFILE) $(PACKAGEFOLDER)/DEBIAN/
    dpkg-deb -b $(PACKAGEFOLDER) $(PACKAGENAME)_$(PACKAGEVERSION)_$(ARCH).deb

# without dpkg-deb
# replace 4 spaces with TAB for below
deb2: stage
    mkdir -p $(PACKAGEFOLDER)/Library/MobileSubstrate/DynamicLibraries/
    mkdir -p $(PACKAGEFOLDER)/DEBIAN
    cp $(CONTROLFILE) $(PACKAGEFOLDER)/DEBIAN/
    rm -f $(PACKAGENAME)_$(PACKAGEVERSION)_$(ARCH).deb $(PACKAGEFOLDER)/control.tar.gz $(PACKAGEFOLDER)/data.tar.gz
    rm -f $(PACKAGEFOLDER)/tmp/*.gz
    mkdir -p $(PACKAGEFOLDER)/tmp
    echo "2.0" > $(PACKAGEFOLDER)/tmp/debian-binary
    cp $(OBJFOLDER)/$(TWEAK_NAME).dylib $(PACKAGEFOLDER)/Library/MobileSubstrate/DynamicLibraries/.
    cd $(PACKAGEFOLDER)/DEBIAN; tar -czf ../tmp/control.tar.gz ./
    cd $(PACKAGEFOLDER); tar -czf ./tmp/data.tar.gz $(_EXCLUDE_COMMANDLINE) ./
    cd $(PACKAGEFOLDER)/tmp; ar -rv ../../$(PACKAGENAME)_$(PACKAGEVERSION)_$(ARCH).deb ./debian-binary ./control.tar.gz ./data.tar.gz
    rm -fr $(PACKAGEFOLDER)/tmp/*.gz


For mobilesubstrate extension, download the iPhone headers from http://github.com/rpetrich/iphoneheaders/archives/master

and place the frameworks at top level (i.e. SpringBoard folder copy to /var/theos/include/SpringBoard).
libsubstrate.dylib is also needed in /var/theos/lib folder

Note: perl is needed for creating project folder and compiling mobilesubstrate extension, if you only develop app and command line tool, you could remove perl by using

apt-get remove perl


(14) This is how to combine Tweak and Settings Bundle in a project
/var/theos/bin/nic.pl MyTweak
and choose [5.] iphone/tweak
/var/theos/bin/nic.pl MyTweakSettings
and choose [3.] iphone/preference_bundle
mv mytweaksettings/*.mm mytweaksettings/Resources mytweaksettings/entry.plist mytweak/

Makefile (Tweak + Bundle) Select all

export THEOS=/var/theos
export GO_EASY_ON_ME=1
include $(THEOS)/makefiles/common.mk

TWEAK_NAME = MyTweak
#MyTweak_FILES = MyTweak.xm
MyTweak_FRAMEWORKS = UIKit

BUNDLE_NAME = MyTweakSettings
MyTweakSettings_FILES = MyTweakSettings.mm
MyTweakSettings_INSTALL_PATH = /System/Library/PreferenceBundles
MyTweakSettings_FRAMEWORKS = UIKit
MyTweakSettings_PRIVATE_FRAMEWORKS = Preferences

ADDITIONAL_CFLAGS = -I"$(SYSROOT)/usr/lib/gcc/arm-apple-darwin10/4.2.1/include"
ADDITIONAL_CPPFLAGS = -I"$(SYSROOT)/usr/include/c++/4.2.1"
ADDITIONAL_CPPFLAGS += -I"$(SYSROOT)/usr/include/c++/4.2.1/armv6-apple-darwin10"

include $(THEOS_MAKE_PATH)/tweak.mk
include $(THEOS_MAKE_PATH)/bundle.mk
include $(THEOS_MAKE_PATH)/aggregate.mk

# replace 4 spaces with TAB for below
internal-stage::
    $(ECHO_NOTHING)mkdir -p $(THEOS_STAGING_DIR)/Library/PreferenceLoader/Preferences$(ECHO_END)
    $(ECHO_NOTHING)cp entry.plist $(THEOS_STAGING_DIR)/Library/PreferenceLoader/Preferences/$(BUNDLE_NAME).plist$(ECHO_END)

# replace 4 spaces with TAB for below
after-stage::
    find $(THEOS_STAGING_DIR) -iname '*.plist' -exec plutil -convert binary1 {} \;
    $(FAKEROOT) chown -R 0:80 $(THEOS_STAGING_DIR)


(15) If you want to test the iOS5 Notification Center Widget
get it from http://github.com/WillFour20/WeeAppTest

git clone git://github.com/WillFour20/WeeAppTest.git

and modified with the following Makefile

Makefile (nc Tweak) Select all

export THEOS=/var/theos
SDKVERSION = 5.0
include $(THEOS)/makefiles/common.mk

BUNDLE_NAME = WeeAppTest
WeeAppTest_FILES = WeeAppTest.mm
WeeAppTest_INSTALL_PATH = /System/Library/WeeAppPlugins/
WeeAppTest_FRAMEWORKS = UIKit CoreGraphics
#WeeAppTest_PRIVATE_FRAMEWORKS = BulletinBoard

ADDITIONAL_CFLAGS = -I"$(SYSROOT)/usr/lib/gcc/arm-apple-darwin10/4.2.1/include"
ADDITIONAL_CPPFLAGS = -I"$(SYSROOT)/usr/include/c++/4.2.1"
ADDITIONAL_CPPFLAGS += -I"$(SYSROOT)/usr/include/c++/4.2.1/armv6-apple-darwin10"

include $(THEOS_MAKE_PATH)/bundle.mk

# replace 4 spaces with TAB for below
after-install::
    install.exec "killall -9 SpringBoard"


and make clean package install

(15) where is IOSurfaceAPI.h in Lion ?

Here
/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/IOSurface.framework/Headers/IOSurfaceAPI.h





iphone-gcc patched package for iPhone 5 / iPad 4 is here below
http://code.google.com/p/apiexplorer/downloads/list