Android abi和CPU位数

3,299 阅读3分钟

前言

不知道大家在设置abi的时候,有没有考虑过这个问题。不同的abi之间有什么区别。32位so又是如何运行在64位机器上的。这个是如何兼容的呢?

从Zygote说起

相信有了解过系统源码的同学都知道这是啥玩意。首先我们看下init进程在启动zygote进程是如何做的。以6.0源码为例。

import /init.environ.rc
import /init.usb.rc
import /init.${ro.hardware}.rc
import /init.${ro.zygote}.rc
import /init.trace.rc

在init.rc文件中,会根据ro.zygote的值去启动响应支持相应位数的进程。那么,ro.zygote有几种取值呢?

如图所示

  • zygote32 仅支持32位
  • zygote64 仅支持64位
  • zygote32_64 兼容32和64,32位优先
  • zygote64_32 兼容模式,64位优先

以小米6为例。属性值如下。

现在看一下init_zygote64_32.rm文件

service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server --socket-name=zygote
    class main
    socket zygote stream 660 root system
    onrestart write /sys/android_power/request_state wake
    onrestart write /sys/power/state on
    onrestart restart media
    onrestart restart netd

service zygote_secondary /system/bin/app_process32 -Xzygote /system/bin --zygote --socket-name=zygote_secondary
    class main
    socket zygote_secondary stream 660 root system
    onrestart restart zygote

可以很清楚的看到,系统在启动的时候,会启动两个Zygote,一个64位一个32位。

Android系统是如何确定应该是fork 64位还是32位的呢

有了解过进程启动的同学应该看到过如下代码。

        synchronized(mLock) {
            return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote);
        }
    private ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {
        Preconditions.checkState(Thread.holdsLock(mLock), "ZygoteProcess lock not held");

        if (primaryZygoteState == null || primaryZygoteState.isClosed()) {
            try {
                primaryZygoteState = ZygoteState.connect(mSocket);
            } catch (IOException ioe) {
                throw new ZygoteStartFailedEx("Error connecting to primary zygote", ioe);
            }
            maybeSetApiBlacklistExemptions(primaryZygoteState, false);
            maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState);
        }
        if (primaryZygoteState.matches(abi)) {
            return primaryZygoteState;
        }

        // The primary zygote didn't match. Try the secondary.
        if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) {
            try {
                secondaryZygoteState = ZygoteState.connect(mSecondarySocket);
            } catch (IOException ioe) {
                throw new ZygoteStartFailedEx("Error connecting to secondary zygote", ioe);
            }
            maybeSetApiBlacklistExemptions(secondaryZygoteState, false);
            maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState);
        }

        if (secondaryZygoteState.matches(abi)) {
            return secondaryZygoteState;
        }

        throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi);
    }
  • 会根据abi的值,先去primaryZygoteState匹配然后,再去匹配备选secondaryZygoteState。

那个,ZygoteState中所存储的abi分类是怎么来的呢?具体细节不说。这里的值是通过读取ro.product.cpu.abilist64,ro.product.cpu.abilist32这两个属性得来的。

#if defined(__LP64__)
static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64";
static const char ZYGOTE_NICE_NAME[] = "zygote64";
#else
static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32";
static const char ZYGOTE_NICE_NAME[] = "zygote";
#endif

        if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) {
            LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.",
                ABI_LIST_PROPERTY);
            return 11;
        }

        String8 abiFlag("--abi-list=");
        abiFlag.append(prop);

将这些值写入参数中,然后跟着ZygoteInit#main方法传入到java层。具体通过socket查询这个值的过程这里就不说了。

现在,看下小米6手机这几个值是多少。

因此,启动的是64位还是32位,重点在于启动进程时的参数abi.

这个ABI是哪里来的

仔细想一想,这个ABI是怎么来的呢?首先,一个通过Launcher启动的应用进程,肯定是不知道应用的ABI的,那么,只能是PMS了,PMS会在开机或者是安装apk的过程中,分析app的abi,然后就可以在启动的时候,fork相应位数的进程。

在PMS#installPackageLI方法中,调用了derivePackageAbi去分析abi.

这个方法的具体逻辑就不多说了,感兴趣的可以去了解下。