Compile the qemu user mode and androidqemu that can be run on Android
Preface
In this article, the qemu project is cross-compiled on Ubuntu 64-bit system, and only the Code related to the qemu user mode is compiled.
"NDK" in the following section means "Android NDK" unless otherwise specified ".
In the following section, "$ NDK" indicates the root directory of the NDK.
Step 1. Download and install Android NDK
The process of downloading and installing Android NDK is not described here.
2. Download qemu
3. Set environment variables of the NDK Tool
Set the Android NDK environment variables for cross-Compilation: NDK and SYSROOT
4. Compile the dependent Library
Glib
Compile the glib library that can be run on Android
Compilation reference: Compile the glib library that can be run on Android
Libpng12
: Https://sourceforge.net/projects/libpng/files/libpng12/
References: Compile the libffi library that can be run on Android
5. Create a soft link for pkg-config
The Source Path in the ln command is the Source Path of the pkg-config tool.
If this soft link is not created, the following error is reported when the configure script is executed:
6. Modify configure
Add PIE support for arm
Find the following code:
Change i386-Linux | x86_64-Linux | x32-Linux | i386-OpenBSD | x86_64-OpenBSD to i386-Linux | x86_64-Linux | x32-Linux | i386-OpenBSD | arm-Linux ".
If you do not do this, use "readelf-S qemu-arm" to view the compiled qemu-arm Executable File segments, we can find that the base address of all the CIDR blocks that can be loaded at runtime is 0x60000000.
There is such a piece of code in configure:
If textseg_addr is the command line option, this command line option specifies the base address of the text segment. After the script, textseg_ldflags will be added to ldflags.
If the base address of the qemu-arm loading segment is 0x60000000, "Segmentation fault" will occur when qemu-arm is running on Android devices ", for details, refer to the steps in the executable ELF File on Android do not have a base address.
7. Run configure
Command Line Parsing
The configure script will output some key information on the terminal, such as the compiler used and flags.
PKG_CONFIG_PATH
In the above command, PKG_CONFIG_PATH = "$ SYSROOT/usr/lib/pkgconfig" is necessary. If you do not set this macro, the configure script outputs "CFLAGS" content as follows:
Follow the path after "-I". First, let's talk about how this path came from. The configure script has the following code:
The "glib_cflags = $ pkg_config -- cflags $ I" statement will obtain the glib inclusion directory. The person reading this article can use this command to view the output content if glib2.0 is installed on the computer: pkg-config-cflags glib-2.0. However, this path is not what I want, because I am currently cross-Compiling and the target is ARM, so here I will set a new pkgconfig directory path to the PKG_CONFIG_PATH macro, enter the following command to view the output content:
Output content:
The path after "-I" is changed.
Note: pkgconfig is a directory that contains information about the dependent libraries installed in step 5.
-Target-list-cpu
-Target-list arm-linux-user indicates that the compiled qemu program is used for user mode and can execute arm commands. The execution environment of the executable program of this arm command is based on the linux system.
-Cpu = arm means that the compiled qemu program can only be executed on the arm machine.
-Disable-system-disable-bsd-user
-Disable-system: do not compile the system mode code.
-Disable-bsd-user: do not compile the bsd user mode code.
-Cross-prefix
Prefix of the Cross-compilation tool. The value in the current command line is "arm-linux-androideabi-", the configure script searches for tools such as arm-linux-androideabi-gcc and arm-linux-androideabi-g ++.
-Disable-tools
When the-disable-tools option is available in the command line, the want_tools macro in the script is set to "no", and the default macro is "yes ". When the want_tools macro is "yes", the tools macro is set. The following is the code for setting the tools macro related to want_tools:
The configure script writes the content of the tools macro to the config-host.mak file.
-Disable-guest-agent
If this option is not available, the following error will be reported during compilation:
This error is not reported when the qemu project is compiled for the PC without this command option. However, the lockf function does not exist on Android, so this error is reported when the qemu project is compiled for Android.
Compilation error troubleshooting ld: error: cannot find-lutil
Comment out the content in the Makefile file in the root directory:
Ifaddrs. h: No such file or directory
Error Message
Solution: download all the source files in this link: android-ifaddrs, and copy the downloaded files to the qga/directory. Find the qga/Makefile. objs file and insert "ifaddrs. o" into the "qga-obj-$ (CONFIG_POSIX)" macro.
Mqueue. h: No such file or directory
Error Message
Solution: Change "# include <mqueue. h>" to "# include <linux/mqueue. h> ".
Char _ unused [128-sizeof (target_sigset_t)];
Error Message
Solution: Change _ unused to _ unused.
Syscall. c: 4108: 9: error: dereferencing pointer to incomplete type
Error Message
Solution
Change
Disas/arm-a64.cc: 67: error: undefined reference to '_ cxa_end_cleanup'
Error Message
Solution: Find the following code in configure:
Comment out the code:
Cause Analysis: currently, Android NDK does not have 64-bit objects.
The symbol cannot be found in syscall. c.
Error Message
Solution: Write the following content in the syscall. c file.
Compile cleanup command
Run the following two commands:
Make clean
Make distclean
Compile the debug version
Add the "-enable-debug" command option to the command line of the configure script.
Author: xunyu @ aliyunju security. For more technical articles, visit the aliyunju security blog.