1. 程式人生 > >交叉編譯e2fsprogs For android

交叉編譯e2fsprogs For android

由於需要使用dumpe2fs debugfs工具,因此交叉編譯了e2fsprogs的原始碼。

本人在ubuntu 14.04下操作的

1. 下載e2fsprogs的原始碼

版本是1.42.6   

2.  下載NDK

https://developer.android.com/ndk/downloads/index.html

3. 生成standalone toolchain (斜體為具體路徑)

cd $NDK_PATH

cd build/tools

./make_standalone_toolchain.sh --install-dir=$TOOLCHAIN_PATH

成功則在$TOOLCHAIN_PATH下生成交叉編譯所需的工具

4. 測試是否成功

cd $TOOL_CHAIN/bin

./arm-linux-androideabi-gcc -v

[email protected]:~/Software/NDK/tool-chain/bin$ ./arm-linux-androideabi-gcc -v
Using built-in specs.
COLLECT_GCC=./arm-linux-androideabi-gcc
COLLECT_LTO_WRAPPER=/home/zombie/Software/NDK/tool-chain/bin/../libexec/gcc/arm-linux-androideabi/4.9.x/lto-wrapper
Target: arm-linux-androideabi
Configured with: /usr/local/google/buildbot/src/android/gcc/toolchain/build/../gcc/gcc-4.9/configure --prefix=/tmp/59719db9ae19ff43aef46bbcb79596b6 --target=arm-linux-androideabi --host=x86_64-linux-gnu --build=x86_64-linux-gnu --with-gnu-as --with-gnu-ld --enable-languages=c,c++ --with-gmp=/buildbot/tmp/build/toolchain/temp-install --with-mpfr=/buildbot/tmp/build/toolchain/temp-install --with-mpc=/buildbot/tmp/build/toolchain/temp-install --with-cloog=/buildbot/tmp/build/toolchain/temp-install --with-isl=/buildbot/tmp/build/toolchain/temp-install --with-ppl=/buildbot/tmp/build/toolchain/temp-install --disable-ppl-version-check --disable-cloog-version-check --disable-isl-version-check --enable-cloog-backend=isl --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --disable-libssp --enable-threads --disable-nls --disable-libmudflap --disable-libgomp --disable-libstdc__-v3 --disable-sjlj-exceptions --disable-shared --disable-tls --disable-libitm --with-float=soft --with-fpu=vfp --with-arch=armv5te --enable-target-optspace --enable-bionic-libs --enable-libatomic-ifuncs=no --enable-initfini-array --disable-nls --prefix=/tmp/59719db9ae19ff43aef46bbcb79596b6 --with-sysroot=/tmp/59719db9ae19ff43aef46bbcb79596b6/sysroot --with-binutils-version=2.25 --with-mpfr-version=3.1.1 --with-mpc-version=1.0.1 --with-gmp-version=5.0.5 --with-gcc-version=4.9 --with-gdb-version=none --with-gxx-include-dir=/tmp/59719db9ae19ff43aef46bbcb79596b6/include/c++/4.9.x --with-bugurl=http://source.android.com/source/report-bugs.html --enable-languages=c,c++ --disable-bootstrap --enable-plugins --enable-libgomp --enable-gnu-indirect-function --disable-libsanitizer --enable-gold --enable-threads --enable-eh-frame-hdr-for-static --enable-graphite=yes --with-isl-version=0.11.1 --with-cloog-version=0.18.0 --with-arch=armv5te --program-transform-name='s&^&arm-linux-androideabi-&' --enable-gold=default
Thread model: posix
gcc version 4.9.x 20150123 (prerelease) (GCC) 
加入PATH中

export PATH=$PATH:/$TOOL_CHAIN/bin

5. 編譯e2fsprogs

cd $e2fsprogs_PATH

export CFLAGS="-static -O2 -fpie -pie"

export LDFLAGS="-staitc -pie"

./configure --host=arm-linux -target=arm-linux CC=arm-linux-androideabi-gcc --prefix=$OUT_DIR

make (-j8)

make install

6. 成功編譯後會在$OUT_DIR/sbin下生成可執行檔案

[email protected]
:~/e2fsprogs/out/sbin$ ls badblocks e2fsck filefrag fsck.ext4 mkfs.ext3 tune2fs blkid e2image findfs fsck.ext4dev mkfs.ext4 uuidd debugfs e2label fsck logsave mkfs.ext4dev dumpe2fs e2undo fsck.ext2 mke2fs mklost+found e2freefrag e4defrag fsck.ext3 mkfs.ext2 resize2fs


7.  剔除可執行檔案符號表資訊

cd $OUT_DIR 

arm-linux-androideabi-strip *

8. 檢視可執行檔案資訊(關鍵是statically linked 和 stripped)

file *

[email protected]:~/e2fsprogs/out/sbin$ file *
badblocks:    ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
blkid:        ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
debugfs:      ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
dumpe2fs:     ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
e2freefrag:   ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
e2fsck:       ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
e2image:      ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
e2label:      ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
e2undo:       ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
e4defrag:     ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
filefrag:     ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
findfs:       ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
fsck:         ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
fsck.ext2:    ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
fsck.ext3:    ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
fsck.ext4:    ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
fsck.ext4dev: ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
logsave:      ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
mke2fs:       ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
mkfs.ext2:    ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
mkfs.ext3:    ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
mkfs.ext4:    ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
mkfs.ext4dev: ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
mklost+found: ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
resize2fs:    ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
tune2fs:      ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
uuidd:        ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, stripped
9. 將可執行檔案放入手機

adb push debugfs /data/local/tmp

10. 在手機中執行

adb shell

cd /data/local/tmp

./debugfs