【ARM汇编】0x00_基于AndroidStudio构建ARM32-v7a以及ARM64-v8a环境

环境准备

安装SDK工具

  • 安装Android Studio
  • SDK Manager中选择NDKAndroid EmulatorAndroid SDK Platform-Tools等进行安装
    ![[Pasted image 20230808011554.png]]
  • SDK文件目录结构
PS C:\Apps\Android\sdk> dir


    目录: C:\Apps\Android\sdk


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          2023/8/8      0:46                .downloadIntermediates
d-----          2023/8/8      0:50                .temp
d-----          2023/8/8      0:50                build-tools
d-----          2023/8/8      0:50                cmake
d-----          2023/8/8      0:50                emulator # 模拟器
d-----          2021/9/3     23:37                extras
d-----          2021/9/4      1:11                fonts
d-----          2021/9/4      0:37                licenses
d-----          2023/8/8      1:28                ndk  # 交叉编译工具链
d-----          2021/9/3     23:36                patcher
d-----          2023/8/8      0:50                platform-tools # adb等
d-----         2023/1/19     20:01                platforms
d-----          2023/8/8      1:43                skins
d-----          2021/9/4      1:09                sources
d-----        2022/12/30     20:32                system-images
d-----          2021/9/3     23:37                tools
-a----          2023/8/8      1:41             16 .knownPackages

配置系统环境变量

配置

ANDROID_USER_HOME控制 .android目录所在位置
默认情况下,模拟器会将配置文件存储在 $HOME/.android/ 下,将 AVD 数据存储在 $HOME/.android/avd/ 下。您可以通过设置以下环境变量来替换默认设置。
emulator -avd <avd_name> 命令会依次按照 $ANDROID_AVD_HOME$ANDROID_USER_HOME/avd/$HOME/.android/avd/ 中的值来搜索 avd 目录。

下表介绍了 Android SDK 工具的常用环境变量。

Android SDK 环境变量
ANDROID_HOME设置 SDK 安装目录的路径。设置后,该值通常不会更改,并且可以由同一台计算机上的多个用户共享。ANDROID_SDK_ROOT 也指向 SDK 安装目录,但已废弃。如果您继续使用它,Android Studio 和 Android Gradle 插件将检查旧变量和新变量是否一致。
ANDROID_USER_HOME为 Android SDK 中包含的工具设置用户偏好设置目录的路径。 默认为 $HOME/.android/

某些较旧的工具(例如 Android Studio 4.3 及更低版本)不会读取 ANDROID_USER_HOME。如需替换这些旧工具的用户偏好设置位置,请将 ANDROID_SDK_HOME 设置为要在其下创建 .android 目录的父目录。
  • ANDROID_HOME环境变量
ANDROID_HOME
C:\Apps\Android\sdk

在这里插入图片描述

  • Path环境变量
%ANDROID_HOME%\tools
%ANDROID_HOME%\platform-tools
%ANDROID_HOME%\emulator
%ANDROID_HOME%\ndk\25.2.9519653  # 这里根据自己的ndk版本自行调整

在这里插入图片描述

测试

  • adb命令
PS C:\Users\xxx> adb
Android Debug Bridge version 1.0.41
Version 34.0.4-10411341
Installed as C:\Apps\Android\sdk\platform-tools\adb.exe
Running on Windows 10.0.22621

global options:
 -a                       listen on all network interfaces, not just localhost
 -d                       use USB device (error if multiple devices connected)
 -e                       use TCP/IP device (error if multiple TCP/IP devices available)
 -s SERIAL                use device with given serial (overrides $ANDROID_SERIAL)
 -t ID                    use device with given transport id
 -H                       name of adb server host [default=localhost]
 -P                       port of adb server [default=5037]
 -L SOCKET                listen on given socket for adb server [default=tcp:localhost:5037]
 --one-device SERIAL|USB  only allowed with 'start-server' or 'server nodaemon', server will only connect to one USB device, specified by a serial number or USB device address.
 --exit-on-write-error    exit if stdout is closed

general commands:
 devices [-l]             list connected devices (-l for long output)
 help                     show this help message
 version                  show version num
  • ndk-build命令
PS C:\Apps\Android\sdk> ndk-build
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
C:\Apps\Android\sdk\ndk\25.2.9519653\build\..\build\core\build-local.mk:151: *** Android NDK: Aborting    .  Stop.

创建ARMv7a模拟器

![[Pasted image 20230808014429.png]]

选择Jelly Bean API Level 16

![[Pasted image 20230808014708.png]]

以下所使用虚拟器所在文件系统路径位于C:\Users\xxx\.android\avd
xxx 代表用户名,后续不再做特别说明

启动模拟器

命令行运行模拟器

emulator -avd Nexus_S_API_16
or 
emulator -avd Nexus_S_API_16 -gpu auto  -grpc-use-jwt

获取设备列表

PS C:\Apps\Android\sdk> adb devices
List of devices attached
emulator-5554   device

adb shell进入命令行

PS C:\Apps\Android\sdk> adb shell
* daemon not running; starting now at tcp:5037
* daemon started successfully
adb.exe: device offline
PS C:\Apps\Android\sdk> adb shell
root@android:/ #

利用NDK交叉编译开发

参考资料: NDK 使用入门 | Android NDK

源码

hello_arm.c

int main(int argc, char** argv)  
{  
    while(1) {  
        printf("Hello ARM.\n");  
        getchar();  
    }  
    return 0;  
}

Application.mk

默认情况下,NDK 构建系统会为所有非弃用 ABI 生成代码。您可以使用 APP_ABI 设置为特定 ABI 生成代码。表 1 显示了不同指令集的 APP_ABI 设置。

指令集
32 位 ARMv7APP_ABI := armeabi-v7a
64 位 ARMv8 (AArch64)APP_ABI := arm64-v8a
x86APP_ABI := x86
x86-64APP_ABI := x86_64
所有支持的 ABI(默认)APP_ABI := all

也可以指定多个值,方法是将它们放在同一行上,中间用空格分隔。例如:

APP_ABI := armeabi-v7a arm64-v8a x86

测试代码:

APP_ABI := armeabi-v7a  # 32 位 ARMv7
APP_BUILD_SCRIPT := Android.mk  
APP_PLATFORM := android-16

Android.mk

默认情况下,构建系统会以 thumb 模式生成 ARM 目标二进制文件,其中每条指令都是 16 位宽,并与 thumb/ 目录中的 STL 库链接。将此变量定义为 arm 会强制构建系统以 32 位 arm 模式生成模块的对象文件。以下示例演示了如何执行此操作:

LOCAL_ARM_MODE := arm

Android.mk代码:

LOCAL_PATH := $(call my-dir)  
LOCAL_MODULE := hello_arm  # 注释就会按照thumb模式编译
LOCAL_SRC_FILES := hello_arm.c  
  
include $(BUILD_EXECUTABLE)  
  
#include $(BUILD_SHARD_LIBRARY)  # 注释了, 不编译成so

ndk-build编译

PS C:\Apps\Android\sdk\tests>  ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk
Android NDK: android-20 is an alias for android-19. Adjusting APP_PLATFORM to match.
[armeabi-v7a] Compile arm    : hello_arm <= hello_arm.c
[armeabi-v7a] Executable     : hello_arm
[armeabi-v7a] Install        : hello_arm => libs/armeabi-v7a/hello_arm

push带debug信息的二进制到文件系统

PS C:\Apps\Android\sdk\tests> adb push C:\Apps\Android\sdk\tests\obj\local\armeabi-v7a\hello_arm /data/user/
C:\Apps\Android\sdk\tests\obj\local\armeabi-v...d, 0 skipped. 2.5 MB/s (5276 bytes in 0.002s)

![[Pasted image 20230827232746.png]]

运行效果

![[Pasted image 20230827233032.png]]
如截图所示,为hello_arm增加可执行权限,运行打印 Hello ARM

IDA汇编调试

上传android_server到模拟器

  • ARM32
PS D:\> adb push D:\IDA_Pro_v7.0_Portable\dbgsrv\android_server /data/user
D:\IDA_Pro_v7.0_Portable\\dbgsrv\android_server: 1 file pushed, 0 skipped. 818.3 MB/s (786868 bytes in 0.001s)
  • ARM64, 则上传android_server64
adb push D:\IDA_Pro_v7.0_Portable\dbgsrv\android_server64 /data/user

PS C:\Apps\Android\sdk> adb shell
root@generic_arm64:/ # cd /data/user/
root@generic_arm64:/data/user # ll
lrwxrwxrwx root     root              1970-01-01 00:00 0 -> /data/data/
-rwxrwxrwx root     root      1243456 2017-09-14 07:08 android_server64
-rwxrwxrwx root     root         7592 2023-10-05 17:27 hello_arm

android_server增加可执行权限


adb shell "chmod 777 /data/user/android_server"

或者进入模拟器执行:

root@android:/data/user # ll
lrwxrwxrwx root     root              1970-01-01 00:00 0 -> /data/data/
-rw-rw-rw- root     root       786868 2020-12-31 16:00 android_server
root@android:/data/user # chmod 777 android_server
root@android:/data/user # ll
lrwxrwxrwx root     root              1970-01-01 00:00 0 -> /data/data/
-rwxrwxrwx root     root       786868 2020-12-31 16:00 android_server

TCP端口映射转发

adb forward tcp:23946 tcp:23946

IDA attach目标进程

在这里插入图片描述

![[Pasted image 20230827235142.png]]

![[Pasted image 20230827235412.png]]

编写命令行脚本

  • adb_shell.bat
adb forward tcp:23946 tcp:23946
  • compile.bat
ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk
  • push_run.bat
adb push C:\Apps\Android\sdk\tests\obj\local\armeabi-v7a\hello_arm /data/user/
adb shell "chmod 777 /data/user/hello_arm"
adb shell "/data/user/hello_arm"

FAQ

[IDA] Bogus or irresponsive remote server

在这里插入图片描述

解决办法:
检查一下自己的端口映射,很可能错了。

正确的:

adb forward tcp:23946 tcp:23946

No gRPC protection active, consider launching with the -grpc-use-jwt flag.

这个警告是 Android Emulator 给出的提醒,提示你当前正在运行的 Android Emulator 没有使用 gRPC 保护,建议使用 "-grpc-use-jwt" 标志启动 Android Emulator。

解决这个问题很简单,你只需要在启动 Android Emulator 的时候加上 "-grpc-use-jwt" 标志即可。

在 Android Studio 中启动 Android Emulator 时,可以在 AVD 管理器中选择要启动的模拟器,然后点击 "Edit",在 "Additional command line options" 中输入 "-grpc-use-jwt",最后点击 "Finish" 启动模拟器。

如果你是在终端中启动 Android Emulator,可以使用以下命令启动模拟器:

emulator @<avd_name> -grpc-use-jwt

举个栗子:  emulator -avd Pixel_3a_API_24 -grpc-use-jwt  -log-nofilter

其中 <avd_name> 是 Android Emulator 虚拟设备的名称。

Unable to connect to adb daemon on port: 5037

说明adb工具还未运行

adb start-server

Avd’s CPU Architecture ‘arm64’ is not supported by the QEMU2 emulator on x86_64 host - Android

https://deycode.com/posts/panic-avds-cpu-architecture-arm64-is-not-supported-by-the-qemu2-emulator-on-x

INFO | Android emulator version 31.3.13.0 (build_id 9189900) (CL:N/A)
emulator: INFO: Found systemPath c:\Users\xxxx\AppData\Local\Android\Sdk\system-images\android-33\google_apis\arm64-v8a\
PANIC: Avd's CPU Architecture 'arm64' is not supported by the QEMU2 emulator on x86_64 host.
The Solutions:

Quick Fix: To run an AVD with arm64 CPU architecture on a Windows 10 machine, you can use an ARM64 CPU or limit the API level to 27 Oreo. Modify the code in the emulator to allow arm64 architecture on x86_64 host.

Solution 1: Limit API level to 27 Oreo

To resolve the issue, you can limit the API level to 27 Oreo. This means that ARM64 emulation on an x86_64 host is only possible up to API level 27. Modify the code in the emulator to allow arm64 architecture on x86_64 host.

Solution 2: Use an ARM64 CPU

Another solution is to use an ARM64 CPU instead of an x86_64 host. This will allow you to run an AVD with arm64 CPU architecture without encountering the error.

Conclusion:

Running an AVD with arm64 CPU architecture on a Windows 10 machine can be achieved by either limiting the API level to 27 Oreo or using an ARM64 CPU. By following these solutions, you can overcome the error and successfully run the AVD with the desired CPU architecture.

Another Solution

https://github.com/google/android-emulator-m1-preview/issues/34

already fixed. just re run darwin-aarch64-replace.sh and everything fine

![[Pasted image 20231001015814.png]]

Download the https://github.com/google/android-emulator-m1-preview/releases/download/0.2/emulator-darwin-aarch64-0.2-engine-only.zip
unzip it
replace emulator and emulator-check to ~/Library/Android/sdk/tools/

OpenGLES emulation failed to initialize

  • 问题描述
1. Make sure your GPU drivers are up to date.

2. Erase and re-download the emulator ($ANDROID_SDK_ROOT/emulator).

3. Try software rendering: Go to Extended Controls > Settings > Advanced tab and change "OpenGL ES renderer (requires restart)" to "Swiftshader".

Or, run emulator from command line with "-gpu swiftshader_indirect". 4. Please file an issue to https://issuetracker.google.com/issues?q=componentid:192727 and provide your complete CPU/GPU info plus OS and display setup.
ERROR   | crashhandler_die: fatal: OpenGLES emulation failed to initialize. Please consider the following troubleshooting steps:

1. Make sure your GPU drivers are up to date.

2. Erase and re-download the emulator ($ANDROID_SDK_ROOT/emulator).

3. Try software rendering: Go to Extended Controls > Settings > Advanced tab and change "OpenGL ES renderer (requires restart)" to "Swiftshader".

Or, run emulator from command line with "-gpu swiftshader_indirect". 4. Please file an issue to https://issuetracker.google.com/issues?q=componentid:192727 and provide your complete CPU/GPU info plus OS and display setup.
  • 解决方法

https://developer.android.google.cn/studio/run/emulator-acceleration?hl=zh-cn

如需使用虚拟机加速,开发环境必须满足以下要求:

  • SDK 工具:最低为版本 17;建议使用版本 26.1.1 或更高版本
  • AVD:下表中介绍了必须满足的 Android 版本要求。

CPU 架构:ARM64 |
系统映像要求: 适用于 Android 5.0(API 级别 21)及更高版本的 arm64-v8a 系统镜像|

emulator -avd API_21 -gpu auto  -grpc-use-jwt

![[Pasted image 20231006012039.png|500]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值