HIPL on Android

Bug #715126 reported by Miika Komu on 2011-02-08
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Pupu Toivonen

Bug Description

HIPL does not build or work on Android yet. Android platform is problematic because it is targeted for java applications. The challenges include:

* DNS proxy is written with python, so it's not possible to port it at all
* The h/w platform for Android is really picky with memory alignments and requires some modifications to the code.
* Many of the libraries are missing, so HIPL has to be cross-compiled and statically linked with some ofthe libraries.

However, the latest Android 2.3 seems to have OpenSSL already available should make the work easier. Please check out (especially the end of the latter link):


Miika Komu (miika-iki) wrote :
Download full text (3.7 KiB)

This is what we have managed so far to complete with a student with a bit older version of HIPL:

1 Download arm-linux tools-chain:
download from http://www.handhelds.org/download/projects/toolchain/arm-linux-gcc-3.4.1.tar.bz2
unzip tar to / directory.
export PATH=$PATH:/usr/local/arm/3.4.1/bin

2 build openssl:
download openssl source code from http://www.openssl.org/source/
./config no-asm shared
Change the Makefile
replace the section started from line 62 with the following:

CC= arm-linux-gcc
EX_LIBS= -ldl
AR= arm-linux-ar $(ARFLAGS) r
RANLIB= arm-linux-ranlib
NM= arm-linux-nm
PERL= /usr/bin/perl
TAR= tar
TARFLAGS= --no-recursion

and then make it!
make install

3 download Android SDK and NDK
unzip ndk into sdk directory.
export ANDROID_ROOT=XXX/android-sdk-linux_86/android-ndk-r3
export PATH=$PATH:XXX/android-sdk-linux_86/android-ndk-r3/build/prebuilt/linux-x86/arm-eabi-4.4.0/bin

copy openssl headers from openssl-1.0.0a/include/openssl directory to $ANDROID_ROOT/build/platforms/android-3/arch-arm/usr/include/ directory
copy libcrypto.so and libcrypto.a libraries to $ANDROID_ROOT/build/platforms/android-3/arch-arm/usr/lib/ directory

4 build HIPL
product configure by run command
autoreconf --install

method 1: use arm-eabi-gcc provided by NDK:
./configure -host=arm-eabi CC=arm-eabi-gcc CPPFLAGS=”-I$ANDROID_ROOT/build/platforms/android-3/arch-arm/usr/include/” CFLAGS=”-nostdlib” LDFLAGS=”-Wl,-rpath-link=$ANDROID_ROOT/build/platforms/android-3/arch-arm/usr/lib/ -L$ANDROID_ROOT/build/platforms/android-3/arch-arm/usr/lib/” LIBS=”-lc “
method 2 (WORKS): use arm-linux-gcc which is a general crosscompile tool:
./configure --disable-gcc-warn --disable-firewall -host=arm-linux CC=arm-linux-gcc CPPFLAGS="-I$OPENSSL/include/" CFLAGS="-nostdlib" LDFLAGS="-Wl,-rpath-link=$ANDROID_ROOT/build/platforms/android-3/arch-arm/usr/lib,-L$ANDROID_ROOT/build/platforms/android-3/arch-arm/usr/lib/" LIBS="-lc"

If you get this far, there are some compilation issues for you to solve:

In file included from ./lib/modularization/lmod.h:37,
                 from lib/core/state.h:37,
                 from lib/core/builder.h:43,
                 from lib/core/builder.c:98:
./lib/core/linkedlist.h:64: warning: declaration of 'index' shadows a global declaration
/home/mkomu/usr/local/arm/3.4.1/bin/../lib/gcc/arm-linux/3.4.1/../../../../arm-linux/sys-include/string.h:267: warning: shadowed declaration is here
./lib/core/linkedlist.h:67: warning: declaration of 'index' shadows a global declaration
/home/mkomu/usr/local/arm/3.4.1/bin/../lib/gcc/arm-linux/3.4.1/../../../../arm-linux/sys-include/string.h:267: warning: shadowed declaration is here
./lib/core/linkedlist.h:70: warning: declaration of 'index' shadows a global declaration


Changed in hipl:
importance: Undecided → Wishlist
Miika Komu (miika-iki) wrote :

The alignment problems can be fixed with:

system("echo 3 > /proc/cpu/alignment");

However, this degrades the performance of the system radically. So, this was solved by a student (on some really old version of Android) using packed data structures. I have some patches from him, but I am not going to publish them online because they do not work with the current data base. Instead, I'll just quote his report:

When you cast unaligned pointer to an aligned type, the gcc takes your word and inlines memcpy. But this will generate unaligned trap, but will work in x86 and other processors where unaligned accesses will be fixed automatically, but not in ARM.

For example, the following code will raise SIGBUS when run in ARM
(in foo(), the types are same, even though we have typecasted it from a packed struct, so gcc inlines the memcpy)

#include <string.h>

typedef struct {
  unsigned int a;
  unsigned int b;
  unsigned char c;
} s;

typedef struct {
  unsigned int a;
  unsigned int b;
  unsigned char c;
} __attribute__ ((packed)) ust;

void foo(s *cp)
  s dst;

  memcpy(&dst, cp, sizeof(s));

  return 0;

int main(int k, char *kk[]) {

  ust tt;
  return foo(&tt);

One place where this was causing a problem was hadb.c::hip_hadb_find_byhits(). There is still one more place where this problem is present: nlink.c::xfrm_fill_selector(), because "struct in6_addr" is packed but not any of the xfrm* structs. But I am not sure if I should modify it, so I have disabled optimization for that lib (libhiptool) for now.

Diego Biurrun (diego-biurrun) wrote :

The compilation problems about shadowed declarations should be trivial to fix. Just rename the "index" variables to "idx".

Miika Komu (miika-iki) on 2011-04-06
Changed in hipl:
assignee: nobody → ibrahim (brah-gharbi)
Miika Komu (miika-iki) wrote :

Rene gave a pointer earlier: the new GCC version may actually help you with HIPL for Android:

Ibraham, feel free to pose questions here too...

Miika Komu (miika-iki) wrote :

Ibrahim will not be able to complete this task.

Changed in hipl:
assignee: ibrahim (brah-gharbi) → nobody
Miika Komu (miika-iki) on 2011-11-01
Changed in hipl:
assignee: nobody → Vidhuran (vidhuran2012)
Miika Komu (miika-iki) wrote :

New up-to-date instructions for Ubuntu *oneiric*. Did not double check the instructions, I hope they are ok from my history. I don't know if there's some unncessary parts in the instructions.

Please note that there's absolutely no reason install android stuff to "/" - everything should be in your home directory. Also, no need to have root privileges for compilation.

# gcc -6 requires a new Ubuntu
mkomu@bling:~/projects/hipl-bzr/arm$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 11.10
Release: 11.10
Codename: oneiric

# let's install it, no need to compile in Oneiric
sudo aptitude install gcc-4.6-arm-linux-gnueabi

# Download and SDK and NDK. Set Android root to make paths shorter later.
cd ~
wget http://dl.google.com/android/android-sdk_r16-linux.tgz
tar xvzf android-sdk_r16-linux.tgz
cd android-sdk-linux
wget http://dl.google.com/android/ndk/android-ndk-r7-linux-x86.tar.bz2
tar xvjf android-ndk-r7-linux-x86.tar.bz2
export ANDROID_ROOT=~/android-sdk-linux/android-ndk-r7
source ~/.bashrc

# Download, compile, modify and install OpenSSL to the NDK directory.
cd ~
wget http://www.openssl.org/source/openssl-1.0.0g.tar.gz
tar xvzf openssl-1.0.0g.tar.gz
cd openssl-1.0.0g
./config no-asm shared --prefix=$ANDROID_ROOT/platforms/android-3/arch-arm/usr
edit Makefile:
  CC= arm-linux-gnueabi-gcc-4.6
  EX_LIBS= -ldl
  AR= arm-linux-gnueabi-ar $(ARFLAGS) r
  RANLIB= arm-linux-gnueabi-ranlib
  NM= arm-linux-gnueabi-nm
  PERL= /usr/bin/perl
  TAR= tar
  TARFLAGS= --no-recursion
make install

# Download and cross-compile HIPL.
cd ~
bzr co lp:hipl trunk
cd trunk
edit configure.ac and comment out all AM_CFLAGS
autoreconf --install
./configure --disable-gcc-warn --disable-firewall -host=arm-linux CC=arm-linux-gnueabi-gcc-4.6 CPPFLAGS="-I$ANDROID_ROOT/platforms/android-3/arch-arm/usr/include" CFLAGS="-nostdlib" LDFLAGS="-Wl,-rpath-link=$ANDROID_ROOT/platforms/android-3/arch-arm/usr/lib,-L$ANDROID_ROOT/platforms/android-3/arch-arm/usr/lib" LIBS="-lc"
make all-am
make[1]: Entering directory `/home/mkomu/projects/hipl-bzr/arm'
  CC lib/core/builder.lo
In file included from lib/core/debug.h:34:0,
                 from lib/core/crypto.h:43,
                 from lib/core/builder.c:100:
lib/core/protodefs.h:917:5: error: unknown type name 'in_port_t'
lib/core/protodefs.h:929:5: error: unknown type name 'in_port_t'
lib/core/protodefs.h:1027:5: error: unknown type name 'in_port_t'
lib/core/protodefs.h:1036:5: error: unknown type name 'in_port_t'
lib/core/protodefs.h:1043:5: error: unknown type name 'in_port_t'
lib/core/protodefs.h:1054:5: error: unknown type name 'in_port_t'
lib/core/protodefs.h:1055:5: error: unknown type name 'in_port_t'
(it appears in_port_t definition is missing from android (?), so it may have to be declared redudantly in some header file)

Miika Komu (miika-iki) on 2013-05-29
Changed in hipl:
assignee: Vidhuran (vidhuran2012) → Juhani Toivonen (scolphoy)
Pupu Toivonen (scolphoy) on 2013-08-02
Changed in hipl:
status: New → In Progress
Pupu Toivonen (scolphoy) wrote :

Just an update, the code doesn't completely compile yet, but this is how I'd initialize a build environment under Ubuntu 13.04 now:

sudo apt-get -y install gcc-4.7-arm-linux-gnueabi autoconf automake libtool make gcc libssl-dev iptables-dev libnet-ip-perl libnet-dns-perl bzr libnetfilter-queue-dev xmlto doxygen check libconfig8-dev fakeroot dpkg-dev debhelper devscripts w3m;

cd ~
wget http://dl.google.com/android/android-sdk_r22.0.5-linux.tgz
tar xvf android-sdk_r22.0.5-linux.tgz
cd android-sdk-linux/
wget http://dl.google.com/android/ndk/android-ndk-r9-linux-x86_64.tar.bz2
tar xvf android-ndk-r9-linux-x86_64.tar.bz2
cd android-ndk-r9/
echo "export ANDROID_ROOT=$(pwd)" >> ~/.bashrc
export ANDROID_ROOT=$(pwd)

cd ~
wget http://www.openssl.org/source/openssl-1.0.0g.tar.gz
tar xvzf openssl-1.0.0g.tar.gz
cd openssl-1.0.0g
./config no-asm shared --prefix=$ANDROID_ROOT/platforms/android-18/arch-arm/usr
wget http://bit.net.co/hipl/droid-openssl-makefile.patch
patch Makefile < droid-openssl-makefile.patch
make install

cd ~
mkdir hipl
cd hipl
bzr branch lp:~hipl-core/hipl/android-port-new
cd android-port-new
autoreconf --install
./configure --disable-gcc-warn --disable-firewall -host=arm-linux CC=arm-linux-gnueabi-gcc-4.7 CPPFLAGS="-I$ANDROID_ROOT/platforms/android-18/arch-arm/usr/include" CFLAGS="-nostdlib -std=gnu99" LDFLAGS="-Wl,-rpath-link=$ANDROID_ROOT/platforms/android-18/arch-arm/usr/lib,-L$ANDROID_ROOT/platforms/android-18/arch-arm/usr/lib" LIBS="-lc"

echo "You are now ready to run 'make' and 'make all-am'"

Miika Komu (miika-iki) wrote :

From Juhani (another way to configure):

./configure --disable-gcc-warn --disable-firewall --host=arm-linux --enable-cross-compile CC=$ANDROID_ROOT/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc CPPFLAGS="-I$ANDROID_ROOT/platforms/android-18/arch-arm/usr/include" CFLAGS="-nostdlib -std=gnu99" LDFLAGS="-Wl,--entry=main,-rpath-link=$ANDROID_ROOT/platforms/android-18/arch-arm/usr/lib,-L$ANDROID_ROOT/platforms/android-18/arch-arm/usr/lib" LIBS="-lc -lgcc";

Miika Komu (miika-iki) on 2014-02-27
Changed in hipl:
status: In Progress → Fix Committed
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers