Android/SharkBait/Building a toolchain for aarch64-linux-android

Title: Building a toolchain for aarch64-linux-android Date: 2018-07-21 1:00 Modified: 2018-07-21 1:00 Category: Android Tags: android, toolchain, gsoc Slug: toolchain-for-aarch64-linux-android Status: published

Preface
To build the Android Platform, we need a toolchain that can produce executables that link correctly against Android's, Bionic. To achieve this, a toolchain targeting  (and  ) is needed. This article aims to provide a step-by-step guide for reproducing the desired toolchain on all architecture that GCC supports running on, including AArch64, which my GSoC project needs. Unfinished work on this topic is documented at the end of this article.

Step 0 -- Clone source and set up environment variables
Clone the following repositories which hold Google's modifications to the GCC toolchain for Android target:


 * Binutils
 * Bionic
 * GCC

The rest of this article assumes that  is in the corresponding project for each step.

Export the environment variables to get the paths right:

Step 1 -- Build and install binutils
Binutils that can process binaries for the target is needed. Create a separate build directory, configure, compile, and then install for target:

The  option is used to work around failed compiles due to newer versions of GCC generating new warnings. Examine  and see if the binutils for the target has been properly installed.

Step 2 -- Install prebuilt libc &amp; headers
The next step, as most cross-compiler creation guides instructs, is to install libc. Unfortunately, we have not figured out a proper way to compile Bionic without Android's (gigantic) build system, so we're just doing a voodoo copy-and-paste. The libc part in this section is expected to get better when we develop a mature solution for building Bionic.

Install  from here. Set up libc and kernel headers.

The following object files are needed for a successful generation of the toolchain:


 * : from NDK
 * : from NDK
 * : from NDK
 * : from NDK
 * : from AOSP
 * : from AOSP
 * : from AOSP
 * : from AOSP

Obtain the above files and place them under  for discovery by the linker.

Step 3 -- Build and install GCC
The final part is relatively simple. Just compile GCC and install it into the toolchain prefix:

Step 4 -- Build and verify &quot;Hello, world!&quot;
We need to verify that the toolchain is really working by creating executables for our target. Write a simple &quot;Hello, world!&quot; program and compile it with:

aarch64-linux-android-g++ hello.cc -o hello -pie -static-libgcc -nostdinc++ -I/usr/local/aarch64-linux-android/include/c++/v1 -nodefaultlibs -lc -lm -lc++ Explanation for the commandline options used:


 * : Android requires Position Independent Executables property for dynamically-linked executables.
 * : Android platform does not have  available; we'll have to make it statically-linked.
 * and : Android uses   as its default STL implementation, and we need this to get the right symbols used by including right C++ headers.
 * This suggests that a correct copy of  headers should be present at the path shown above.
 * and : by default GCC links to , which is not desirable in this case; we manually specify what to consider during the linking process.
 * This suggests that  should be present in the linker search path.

What else?
The work is not finished yet on this topic:


 * We still copy-and-paste  object files instead of properly building them separately. Proper packaging of   is needed.
 * The toolchain build process needs integrating with, Gentoo's flexible cross-compile toolchain generator.