From 9f08ccbcc436eab09bbd03cafa576f9ee8a7f037 Mon Sep 17 00:00:00 2001 From: Daniel Corak Date: Fri, 3 Nov 2017 19:53:11 +0100 Subject: [PATCH] Build radare2 for all iOS archs and the simulator (#8775) * Fix system() not available for iOS Simulator * Embedding Bitcode should be set in sys/ios-sdk.sh. Removed unused code * Supports now all available iOS archs and the simulator --- shlr/spp/config.def.h | 2 +- sys/ios-sdk-gcc | 77 +------------- sys/ios-sdk.sh | 234 ++++++++++++++++++++++++++++-------------- 3 files changed, 161 insertions(+), 152 deletions(-) diff --git a/shlr/spp/config.def.h b/shlr/spp/config.def.h index 49001fa155..81395a87b1 100644 --- a/shlr/spp/config.def.h +++ b/shlr/spp/config.def.h @@ -3,7 +3,7 @@ #endif #if HAVE_FORK -# if TARGET_OS_IPHONE || APPLE_SDK_IPHONEOS +# if TARGET_OS_IPHONE || APPLE_SDK_IPHONEOS || APPLE_SDK_IPHONESIMULATOR # define HAVE_SYSTEM 0 # else # define HAVE_SYSTEM 1 diff --git a/sys/ios-sdk-gcc b/sys/ios-sdk-gcc index e65527f87d..946ee490b1 100755 --- a/sys/ios-sdk-gcc +++ b/sys/ios-sdk-gcc @@ -1,14 +1,11 @@ #!/bin/sh [ -z "${CPU}" ] && CPU="arm64" -# CPU=arm64 CWD="$(dirname "$0")" [ -z "${IOSINC}" ] && IOSINC="${CWD}/ios-include" -#CC="xcrun --sdk iphoneos gcc -arch armv7 -I${CWD}/ios-include" CPUS="" CPU=`echo $CPU | sed -e 's,+, ,g'` -#EXTRA="" -EXTRA="-fembed-bitcode" +EXTRA="" for a in `IFS=+ echo ${CPU}` ; do CPUS="-arch $a ${CPUS}" done @@ -16,79 +13,9 @@ done APPLE_SDK=`echo ${SDK} |tr a-z A-Z` CC="xcrun --sdk ${SDK} clang ${CPUS} -DAPPLE_SDK_${APPLE_SDK}=1 -I${IOSINC} ${EXTRA}" echo $CC $* -#CC="xcrun --sdk iphoneos gcc -arch arm64" ${CC} $* r=$? if [ "$r" != 0 ]; then echo ${CC} $* >&2 fi -exit $r - - -##################################### LEGACY AND UNUSED CODE ########################################## -export PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:$PATH - -#CPU="armv7 arm64" -if [ -z "${CPU}" ]; then - CPU=armv7 - #CPU=arm64 -fi -if [ -z "${IOSVER}" ]; then - IOSVER=9.0 -fi - -# USE_SIMULATOR=1 -if [ -z "${USE_SIMULATOR}" ]; then - USE_SIMULATOR=0 -fi - -DEVROOT=/Applications/Xcode.app/Contents/Developer -if [ "${USE_SIMULATOR}" = 1 ]; then -CPU=i386 -CFLAGS="${CFLAGS} -miphoneos-version-min=7.0" -SDKROOT=${DEVROOT}/Platforms/iPhoneSimulator.platform -IOSROOT=${SDKROOT}/Developer/SDKs/iPhoneSimulator${IOSVER}.sdk -CFLAGS="${CFLAGS} -isysroot ${SDKROOT} -I${SDKROOT}" -LDFLAGS="-static" -else -SDKROOT=${DEVROOT}/Platforms/iPhoneOS.platform -IOSROOT=${SDKROOT}/Developer/SDKs/iPhoneOS${IOSVER}.sdk -CFLAGS="-isysroot ${SDKROOT} -I${SDKROOT} -D__arm=1" -fi - -LDFLAGS="-isysroot ${SDKROOT}" -# -Wl, -syslibroot ${SDKROOT}" -export PATH=${DEVROOT}/usr/bin:${SDKROOT}/Developer/usr/bin:${PATH} -CC=${DEVROOT}/usr/bin/gcc -CC="xcrun --sdk iphoneos gcc" -CXX="xcrun --sdk iphoneos g++" -LD=${CC} -export CC CXX LD -for a in ${CPU} ; do - CFLAGS="${CFLAGS} -arch ${a}" -done -if [ "${USE_SIMULATOR}" != 1 ]; then - if [ -z "${IOSINC}" ]; then - IOSINC=${CWD}/ios-include - IOSINC=/Users/pancake/prg/radare2/sys/ios-include - fi - if [ -n "${IOSINC}" ]; then - CFLAGS="${CFLAGS} -I${IOSINC}" - fi -fi -CFLAGS="${CFLAGS} -I${IOSROOT}/usr/include" -#CFLAGS="${CFLAGS} -I${IOSROOT}/usr/include" -LDFLAGS="-L ${IOSROOT}/usr/lib/system ${LDFLAGS}" -LDFLAGS="-L ${IOSROOT}/usr/lib/ ${LDFLAGS}" - -# Workaround -#if [ ! -e ${IOSROOT}/usr/lib/system/libcrypto.dylib ]; then -#sudo cp \ -# ${IOSROOT}/usr/lib/system/libcorecrypto.dylib \ -# ${IOSROOT}/usr/lib/system/libcrypto.dylib -#fi - -# arm64 -# shellcheck disable=SC2048 -# shellcheck disable=SC2086 -${CC} "${CFLAGS}" "${LDFLAGS}" $* +exit $r \ No newline at end of file diff --git a/sys/ios-sdk.sh b/sys/ios-sdk.sh index bed61f12dc..75f3663a40 100755 --- a/sys/ios-sdk.sh +++ b/sys/ios-sdk.sh @@ -1,28 +1,30 @@ #!/bin/sh -DEFCPU=arm64 -BUILD=1 +# You can modify these variables PREFIX="/usr" -# PREFIX=/var/mobile +INSTALL_DST="/tmp/r2ios" +USE_SIMULATOR=0 +SIMULATOR_ARCHS="i386+x86_64" +PACKAGE_RADARE=0 +EMBED_BITCODE=0 +CFLAGS="-O2" +ARCHS="" # Will be set by -archs argument. If you want to set it -> e.g. ARCHS="armv7+arm64". +MERGE_LIBS=1 # Will merge libs if you build for arm and simulator -iosEnviron() { - [ -z "$CPU" ] && CPU="$DEFCPU" +# Environment variables +export PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:$PATH +export PATH=`pwd`/sys:${PATH} +export CC=`pwd`/sys/ios-sdk-gcc +export LD="xcrun --sdk iphoneos ld" +export IOSVER=9.0 +export IOSINC=`pwd`/sys/ios-include +export USE_IOS_STORE=1 - export CPU="$CPU" - echo CPU=$CPU - export PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:$PATH - export PATH=`pwd`/sys:${PATH} - export CC=`pwd`/sys/ios-sdk-gcc - export RANLIB="xcrun --sdk iphoneos ranlib" - export LD="xcrun --sdk iphoneos ld" - # set only for arm64, otherwise it is armv7 - # select ios sdk version - export IOSVER=9.0 - export IOSINC=`pwd`/sys/ios-include - export CFLAGS="-O2 -fembed-bitcode -arch armv7" - export USE_SIMULATOR=0 - export USE_IOS_STORE=1 -} +if [ "${EMBED_BITCODE}" = 1 ]; then + export CFLAGS="$CFLAGS -fembed-bitcode" +else + export CFLAGS=$CFLAGS +fi iosConfigure() { if [ "${USE_IOS_STORE}" = 1 ]; then @@ -33,7 +35,6 @@ iosConfigure() { ./configure --prefix=${PREFIX} --with-ostype=darwin \ --without-pic --with-nonpic --without-fork \ --with-compiler=ios-sdk --target=arm-unknown-darwin - # --disable-debugger --with-compiler=ios-sdk return $? } @@ -42,23 +43,31 @@ iosClean() { } iosBuild() { - time make -j4 || exit 1 - # Build and sign - ( cd binr/radare2 ; make ios_sdk_sign ) - return $? + time make -j4 || exit 1 + # Build and sign + ( cd binr/radare2 ; make ios_sdk_sign ) + make install DESTDIR=$INSTALL_DST + rm -rf $INSTALL_DST/$PREFIX/share/radare2/*/www/enyo/node_modules + return $? +} + +iosMergeLibs() { + mkdir $INSTALL_DST/$PREFIX/lib_merged + echo "\\nMerging dynamic libs" + lipo $INSTALL_DST/$PREFIX/lib/libr*git.dylib $INSTALL_DST/$PREFIX/lib_simulator/libr*git.dylib -output $INSTALL_DST/$PREFIX/lib_merged/libr2.dylib -create + echo "Merging static libs (only libr.a)" + lipo $INSTALL_DST/$PREFIX/lib/libr.a $INSTALL_DST/$PREFIX/lib_simulator/libr.a -output $INSTALL_DST/$PREFIX/lib_merged/libr.a -create + echo "\\nYou can find the merged libs in $INSTALL_DST$PREFIX/lib_merged" } iosPackage() { - rm -rf /tmp/r2ios - make install DESTDIR=/tmp/r2ios - rm -rf /tmp/r2ios/usr/share/radare2/*/www/enyo/node_modules - ( cd /tmp/r2ios && tar czvf ../r2ios-${CPU}.tar.gz * ) + ( cd $INSTALL_DST && tar czvf $INSTALL_DST-${CPU}.tar.gz * ) # Prepare radare2 rm -rf sys/cydia/radare2/root rm -rf sys/cydia/radare2/root/usr/lib/*.dSYM rm -rf sys/cydia/radare2/root/usr/lib/*.a mkdir -p sys/cydia/radare2/root - sudo tar xpzvf /tmp/r2ios-${CPU}.tar.gz -C sys/cydia/radare2/root + sudo tar xpzvf $INSTALL_DST-${CPU}.tar.gz -C sys/cydia/radare2/root rm -rf sys/cydia/radare2-dev/root # Prepare radare2-dev mkdir -p sys/cydia/radare2-dev/root @@ -76,55 +85,128 @@ iosPackage() { return $? } -###################################################################### +showHelp() { + echo "The following arguments are available:" -#if [ ! -d sys/ios-include ]; then -#( -# cd sys && \ -# curl -o ios-include.tar.gz http://lolcathost.org/b/ios-include.tar.gz && \ -# tar xzvf ios-include.tar.gz -#) -#fi -case "$1" in -arm|armv7) - CPU=armv7 - shift - ;; -arm64|aarch64) - CPU=arm64 - shift - ;; --p) - iosPackage + echo "\\n\\t-archs" + echo "\\t\\tSelect the archs, you want to build for." + echo "\\t\\tAvailable archs: armv7, armv7s, arm64, all" + echo "\\t\\tYou can select multiple archs by concatenating" + echo "\\t\\tthem with a '+' sign." + echo "\\t\\tOr specify 'all' to build for armv7+armv7s+arm64." + echo "\\t\\tSee the examples below." + + echo "\\n\\t-h, --help" + echo "\\t\\tShow this text." + + echo "\\n\\t-p" + echo "\\t\\tPackage radare2." + + echo "\\n\\t-s" + echo "\\t\\tRun shell." + + echo "\\n\\t-simulator" + echo "\\t\\tBuild also for i386 and x86_64 archs." + echo "\\t\\tSo you can use radare2 in the iOS simulator." + + echo "\\nExamples:" + echo "\\tsys/ios-sdk.sh -archs arm64" + echo "\\tsys/ios-sdk.sh -archs armv7+arm64 -simulator" + echo "\\tsys/ios-sdk.sh -archs all -simulator" + + echo "\\nYou can also modify some variables in sys/ios-sdk.sh." +} + +# Show help text, if no archs are selected +if [ $# -eq 0 ] && [ "${#ARCHS}" = 0 ] && [ "${USE_SIMULATOR}" = 0 ]; then + echo "You need to specify the archs you want to build for." + echo "Use the -archs/-simulator argument or modify the ARCHS/USE_SIMULATOR variable in sys/ios-sdk.sh.\\n" + showHelp exit 0 - ;; --s) - : - ;; -'') - CPU=arm64 - ;; -*) - echo "Valid values for CPU are: armv7 or arm64 (add -s to start a shell)" - echo "Run 'sys/rebuild.sh iosdbg' for quick rebuilds for the debugger" - exit 1 -esac - -if [ "$1" = -s ]; then - export PS1="\033[33m[ios-sdk-$CPU \w]> \033[0m" - exec $SHELL - exit $? fi -echo -echo "BUILDING R2 FOR iOS CPU = $CPU" -echo -sleep 1 +while test $# -gt 0; do + case "$1" in + -archs) + shift + if test $# -gt 0; then + if [ "$1" == "all" ]; then + ARCHS="armv7+armv7s+arm64" + else + ARCHS=$1 + fi + fi + shift + ;; + -p) + iosPackage + exit 0 + ;; + -s) + export PS1="\033[33m[ios-sdk-$CPU \w]> \033[0m" + exec "$SHELL" + exit $? + ;; + -simulator) + USE_SIMULATOR=1 + shift + ;; + *|-h|--help) + showHelp + exit 0 + ;; + esac +done -iosEnviron -iosClean -iosConfigure -if [ $? = 0 ]; then - iosBuild - iosPackage + +### BUILD PHASE + +# Print which archs we are building for +echo "Will build for \\c" +if [ "${#ARCHS}" -gt 0 ]; then + echo "$ARCHS \\c" + [ "${USE_SIMULATOR}" = 1 ] && echo "and \\c" fi +[ "${USE_SIMULATOR}" = 1 ] && echo "simulator($SIMULATOR_ARCHS)" +echo +sleep 2 + +rm -rf $INSTALL_DST + +# Build radare2 for xi386 and x86_64 +if [ "${USE_SIMULATOR}" = 1 ]; then + iosClean + iosConfigure + if [ $? = 0 ]; then + export CPU="$SIMULATOR_ARCHS" + export SDK=iphonesimulator + echo "\\nBuilding for simulator($SIMULATOR_ARCHS)\\n" + sleep 1 + iosBuild + # backup lib folder of simulator + cp -r $INSTALL_DST/$PREFIX/lib $INSTALL_DST/$PREFIX/lib_simulator + fi +fi + +# check if arm archs were selected and if so build radare2 for them +if [ "${#ARCHS}" -gt 0 ]; then + iosClean + iosConfigure + if [ "$?" = 0 ] && [ "${#ARCHS}" -gt 0 ]; then + export CPU=$ARCHS + export SDK=iphoneos + echo "\\nBuilding for $CPU\\n" + sleep 1 + iosBuild + if [ "${PACKAGE_RADARE}" = 1 ]; then + iosPackage + fi + fi +fi + +# Merge libs if built for simulator and arm archs +if [ "${MERGE_LIBS}" = 1 ]; then + if [ "${USE_SIMULATOR}" = 1 ] && [ "${#ARCHS}" -gt 0 ]; then + iosMergeLibs + fi +fi \ No newline at end of file