Samveen's Blog

22nd August 2013

Bash For Android

There have been multiple attempts to build Bash for Android, most of them successful, yet most instances are compiled statically using non-NDK tool-chains. That seems somewhat wrong to me(See my previous post on Strace for Android). Conversely, given that the Bionic libc is somewhat limited, there are issues with building most software using the NDK.

In comes the Crystax NDK, or The Improved Android NDK by Crystax. The latest version is r8 (which matches Google's NDK r8). The rockstars at Crystax added some extra sugar on top of the NDK (as a static libcrystax) which provides some of the most commonly used standard library functions missing from Bionic.

However, this is not the silver bullet(or pullet, if I remember the Looney Toons correctly). The bash configure script is a different beast altogether (actually any configure script) when cross compiling. Tests that require probing the target system or running commands on the target system will fail during a cross compile. It makes me ask, "Were Art Thou, NDK on Android?".

Exclamations aside, the below was my process to build a dynamically linked Bash for Android.

  • Get Bash-4.2 from the GNU mirrors.
  • Run configure as follows:
    $ export NDKROOT=$HOME/ndk
    $ export SYSROOT=$HOME/ndk/platforms/android-8/arch-arm
    $ export HWKIND=generic
    $ export PATH="$PATH:$NDKROOT/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin/" 
    $ ./configure --host=armv7-unknown-linux-androideabi \
    >     --target=armv7-unknown-linux-androideabi \
    >     --build=x86_64-linux-gnu --prefix=/data/local/ \
    >     host_alias=arm-linux-androideabi \
    >     "CFLAGS=--sysroot=$SYSROOT -Wall -Wextra  -I$NDKROOT/sources/crystax/include -L$NDKROOT/sources/crystax/libs/armeabi-v7a" \
    >     CPP=arm-linux-androideabi-cpp CC=arm-linux-androideabi-gcc \
    >     LD=arm-linux-androideabi-ld RANLIB=arm-linux-androideabi-ranlib \
    >     AR=arm-linux-androideabi-ar
  • Epic fail on make with prototype errors for vprintf
  • Modify config.h as follows:
    < /* #undef HAVE_VPRINTF */
    > #define HAVE_VPRINTF 1
  • On examining config.h, realise that config.h is incorrect due to the cross compile, and fix some of the glaring issues:
    < /* #undef HAVE_MKFIFO */
    > #define HAVE_MKFIFO 1
    < #define JOB_CONTROL_MISSING 1
    > /* #undef JOB_CONTROL_MISSING */
  • Minor fail on make, due to linker errors on getpwent and setpwent
  • Google for solution and discover this page, which contains a set of patches to build Bash on Android with the NDK.
  • Get and apply all patches to the code.
  • Rerun configure
  • Re-edit config.h
  • Rerun make and minor fail on missing flag -lm
  • Rerun configure after adding -lm into the CFLAGS variable
  • Re-edit config.h
  • Rerun make For The Win
  • Run arm-linux-androideabi-strip bash to strip the executable.
  • Ship to device and Enjoy

15th June 2013

Strace on Android

I love strace. The ability to trace a process's interaction with the underlying system is absolutely essential, if not an amazing way to understand the facilities the system provides. I can safely say that more than 50% of my knowledge of Linux's system calls comes from using strace on all sorts of programs (particularly gcc). Given that I'm trying to find/receive/build a native hosted gcc on Android, I want strace on Android as well.

There are quite a lot of resources online about compiling strace for Android, However, most of them suffer from a fatal flaw. They don't use the Android NDK to build strace, instead using the awesome standalone CodeSourcery tool chains to build it. However, the CodeSourcery tool chains are targeted at Linux with the GNU libc, requiring that strace be statically compiled to ensure that it runs on Android, which is packaged with Bionic libc. However, Android /Linux does differ from Linux in such subtle ways that even I am unsure of what they are(apart from the fact that there ARE differences in the system calls). This means that any strace not compiled by the NDK _may_ provide erroneous info about the system calls from the process being traced.

So, I decided to try and build strace using the NDK. I got strace-5.8 from, proceeded to ./configure with the correct CC and --sysroot combination and then ran make. The failure of the build on missing headers was the beginning of my problems. After ensuring that the required definitions were found using the appropriate replacement headers, the kicker came in the form of functions missing from Bionic. This meant that I was stuck. It also meant that strace unmodified would not compile using the NDK.

What saved my day was this interesting post from Lucas Tan detailing the method to build strace using the same infrastructure as that used to build the Android kernels. What caught my eye was the git repository platform/external/strace on which meant that there was a code base of strace fixed up for android. A quick git clone, a little renaming magic, a couple of -D compiler defines, and I had a nice working strace 5.6 binary (so what if it's not the latest version - 5.8).

22nd April 2012

Building File for Android

One important forensic tool on the POSIX systems is file, and has been for at least 20 years. So when I work on an android host, it is disconcerting to find that tool missing.

After wandering around the Infobahn[3] looking for an android build of the file command, I finally went ahead and compiled it myself.

The package containing the file binary (v5.11), the associated magic files and the build specific files that I needed (over and above the source) to build it can be found here.

Source: file website
Compile pointers: Native Vim on Android
[3] And the nominees for best Internet synonyms are...

29th April 2011

PHP Quote Escaping Stupidity

I use the excellently minimalistic hlscript by Brennan Underwood for blogging. However, due to it's minimalism, it lacks a blog entry creation UI. All entries have to be written in or pushed to the server via the terminal. Additionally I lose the ability to preview the post.

So I decided to hack up a small entry creator script that allows me to preview and then submit each post. Nothing very polished nor integrated into hlscript, just a stand alone script that can create a file in the entry format used by hlscript.

While writing and testing this script, I found out two important things. First, irritatingly, PHP escaped all quotes automatically and second, more irritatingly although off topic, I've lost all touch with PHP and web coding, . This auto-escaping of quotes leads to a doubling of backslashes every time I press preview. A quick search gives me the solution as explained here, with a nice example use case here. Essentially

if(get_magic_quotes_gpc()) {
    $value = stripslashes($value);
7th April 2011

Vim on Android

I recently got myself an android based phone (An Aircel India branded Huawei Ideos U8150) and was pretty impressed with it. The fact that I could open a terminal into it and pretty much use it as I would my laptop or Desktop, was mind-blowing to me. Further, as I got it on a lark without looking into the specs of the phone, it felt all the more amazing.

I decided to go for broke and use it as my perl development system. Requirements: A terminal, Perl base, and Vim. The terminal was easy to trace. A simple search on "Best android terminal" through enough lists to convince me that the open source "ConnectBot" was good enough. Getting the package URL on to the phone was the stupid problem, luckily easily solved (steps in Appendix 1). Next came the perl install. After some searching, I found the Scripting Layer for Android or SL4A and got my perl installation up and running. The last and most problematic problem was the install of Vim. After a couple of searches, I found the tarball of compiled binary distribution of Vim at . The instructions with the package required root access on the phone. I decided to install it on the sdcard, instead of the recommended location. I created a pseudo-home at /mnt/sdcard/home, created an opt at /mnt/sdcard/opt, extracted the tar's bin and share dirs into it, and started up vim.

To my dismay, it failed to startup.The problem with my method is that the sdcard is always mounted with the option 'noexec'. I did need root access. Thanks to the wonderful app "z4root" by RyanZA (The rageaginsthtecage exploit is amazing and Sebastian Krahmer is a God ( ) ), I got myself root access on the phone. First thing is that I remounted the sdcard without 'noexec'. Next, i tried vim. Voila, it worked. With proper guidance in the form of the environment variables TERMINFO and VIMRUNTIME , I got vim up and running like a charm (and full syntax and arrow movement).

Caveats: you need to add perl binary to the path or link to it somehow (Location: /data/data/com.googlecode.perlforandroid/files/perl/perl).

Appendix 1:
Barcode Scanner App from the xzing team ( and the QR generator page which uses the Google Chart API (alternately ). Generate QR of URL/text, scan with barcode scanner app, and you've got it on your phone. A must have application if I ever saw one.

7th August 2006

Musings on Belief

I read somewhere that nostalgia isn't a healthy thing because the good is highlighted while the bad is glazed over. But this doesn't stop me from being nostalgic. The best time of my life, defining it in terms of how I felt about myself, would be mid 2003 to mid 2004. That was the time when I had the most confidence in myself. I believed myself capable of any thing I wanted to do and indeed I was. I wanted a good score in GRE, and I got it. I wanted to get through to a US NEWS top 50 Univ in CS and I got through to U of Utah, Salt Lake City. I wanted to get through to an IIT, I got through to 2. Many other small things abound that cemented that belief. Then I came to IIT. Here things started to turn out not quite as good as they should have. Being a adolescent mentally (that's how i catagorize myself), I went for the frivolous pleasures of IIT life rather than the more meaningful ones. Two years down the line, I look back and see that I do not have as much confidence in myself as I did 2 years ago. This erosion is harmful in itself because it leads me to question my abilities, thus leading into a viscious cycle. But I cannot stop thinking in these terms.

Therein lies my dilemma. I, for the love of God, am unable to decide whether my percieved loss of abilities is actually a loss of self confidence or is it that I was actually less capable than I believed. That is the curse of self doubt, and conversely the boon of self confidence. When I believed myself capable of anything, I did just that. Now I doubt myself and thus unable to do the important things that I need to do.

I hate this, the curse of being "me". That does not mean I hate myself though. Infact I am rather glad with the way my life has turned out till now, despite all it's hiccups. That is, I could give up my life right now without qualms as I believe that I have done all in life that needs to be done. Hah, there is that nostalgia again, albeit in a slightly different form.

21st October 2010

The Beginning

This is the beginning of blogging again.