1#!/bin/bash 2# 3# honggfuzz capstone build help script 4# ----------------------------------------- 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18if [ -z "$NDK" ]; then 19 # Search in $PATH 20 if [[ $(which ndk-build) != "" ]]; then 21 NDK=$(dirname $(which ndk-build)) 22 else 23 echo "[-] Could not detect Android NDK dir" 24 exit 1 25 fi 26fi 27 28if [ $# -ne 2 ]; then 29 echo "[-] Invalid arguments" 30 echo "[!] $0 <CAPSTONE_DIR> <ARCH>" 31 echo " ARCH: arm arm64 x86 x86_64" 32 exit 1 33fi 34 35readonly CAPSTONE_DIR=$1 36 37# Fetch if not already there 38if [ ! -d $CAPSTONE_DIR ]; then 39 echo "[!] capstone not found. Fetching a fresh copy" 40 git clone https://github.com/aquynh/capstone $CAPSTONE_DIR 41fi 42 43case "$2" in 44 arm|arm64|x86|x86_64) 45 readonly ARCH=$2 46 if [ ! -d $CAPSTONE_DIR/$ARCH ] ; then mkdir -p $CAPSTONE_DIR/$ARCH; fi 47 ;; 48 *) 49 echo "[-] Invalid CPU architecture" 50 exit 1 51 ;; 52esac 53 54case "$ARCH" in 55 arm) 56 CS_ARCH="arm" 57 CS_BUILD_BIN="./make.sh cross-android $ARCH" 58 ;; 59 arm64) 60 CS_ARCH="arm aarch64" 61 CS_BUILD_BIN="./make.sh cross-android $ARCH" 62 ;; 63 x86) 64 CS_ARCH="x86" 65 CS_BUILD_BIN="make" 66 TOOLCHAIN=i686-linux-android 67 TOOLCHAIN_S=x86-4.9 68 ;; 69 x86_64) 70 CS_ARCH="x86" 71 CS_BUILD_BIN="make" 72 TOOLCHAIN=x86_64-linux-android 73 TOOLCHAIN_S=x86_64-4.9 74 ;; 75esac 76 77# Capstone handles internally only Android ARM cross builds not Intel x86/x86_x64 78# We need to prepare the Android NDK toolchains manually for these builds 79if [[ "$ARCH" == "x86" || "$ARCH" == "x86_64" ]]; then 80 if [ -z "$NDK" ]; then 81 # Search in $PATH 82 if [[ $(which ndk-build) != "" ]]; then 83 $NDK=$(dirname $(which ndk-build)) 84 else 85 echo "[-] Could not detect Android NDK dir" 86 exit 1 87 fi 88 fi 89 90 # Support both Linux & Darwin 91 HOST_OS=$(uname -s | tr '[:upper:]' '[:lower:]') 92 HOST_ARCH=$(uname -m) 93 94 SYSROOT="$NDK/platforms/android-21/arch-$ARCH" 95 export CC="$NDK/toolchains/$TOOLCHAIN_S/prebuilt/$HOST_OS-$HOST_ARCH/bin/$TOOLCHAIN-gcc --sysroot=$SYSROOT" 96 export CXX="$NDK/toolchains/$TOOLCHAIN_S/prebuilt/$HOST_OS-$HOST_ARCH/bin/$TOOLCHAIN-g++ --sysroot=$SYSROOT" 97 export PATH="$NDK/toolchains/$TOOLCHAIN_S/prebuilt/$HOST_OS-$HOST_ARCH/bin":$PATH 98 # We need to construct a cross variable that capstone Makefile can pick ar, strip & ranlib from 99 export CROSS="$NDK/toolchains/$TOOLCHAIN_S/prebuilt/$HOST_OS-$HOST_ARCH/bin/$TOOLCHAIN-" CFLAGS="--sysroot=$SYSROOT" LDFLAGS="--sysroot=$SYSROOT" 100fi 101 102# Change workdir to simplify args 103cd $CAPSTONE_DIR 104 105# Build it 106make clean 107 108NDK=$NDK CAPSTONE_BUILD_CORE_ONLY=yes CAPSTONE_ARCHS=$CS_ARCH \ 109CAPSTONE_SHARED=no CAPSTONE_STATIC=yes \ 110eval $CS_BUILD_BIN 111if [ $? -ne 0 ]; then 112 echo "[-] Compilation failed" 113 exit 1 114else 115 echo "[*] '$ARCH' libcapstone available at '$CAPSTONE_DIR/$ARCH'" 116fi 117 118cp libcapstone.a $ARCH/ 119 120# Revert workdir to caller 121cd - &>/dev/null 122