android ndk ABI Management

ABI Management

CPU/ABI :模拟器要运行的系统印象文件

Different Android handsets(手机,装置) use different CPUs, which in turn support different instruction sets指令集. Each combination of CPU and instruction sets has its own Application Binary Interface, or ABI. The ABI defines, with great precision精确的, how an application's machine code is supposed假定 to interact相互作用 with the system at runtime. You must specify an ABI for each CPU architecture you want your app to work with.

A typical ABI includes the following information:

  • The CPU instruction set(s) that the machine code should use.
  • The endianness字节次序 of memory stores and loads at runtime.
  • The format of executable binaries, such as programs and shared libraries, and the types of content they support.
  • Various conventions规定约定 for passing data传递数据 between your code and the system. These conventions include alignment constraints对齐制约, as well as how the system uses the stack and registers when it calls functions.
  • The list of function symbols available to your machine code at runtime, generally from very specific sets of libraries.

This page enumerates列举 the ABIs that the NDK supports, and provides information about how each ABI works.

Supported ABIs


Each ABI supports one or more instruction sets指令集. Table 1 provides an at-a-glance overview of the instruction sets each ABI supports.

Table 1. ABIs and supported instruction sets.

ABI Supported Instruction Set(s) Notes
armeabi
  • ARMV5TE and later
  • Thumb-1
No hard float.
armeabi-v7a(armeabi-v7a-hard)
  • armeabi
  • Thumb-2
  • VFPv3-D16
  • Other, optional
Hard float when specified as armeabi-v7a-hard. Incompatible with ARMv5, v6 devices.
arm64-v8a
  • AArch-64
x86
  • x86 (IA-32)
  • MMX
  • SSE/2/3
  • SSSE3
No support for MOVBE or SSE4.
x86_64
  • x86-64
  • MMX
  • SSE/2/3
  • SSSE3
  • SSE4.1, 4.2
  • POPCNT
mips
  • MIPS32r1 and later
Uses hard-float, and assumes a CPU:FPU clock ratio of 2:1 for maximum compatibility. Provides neither micromips nor MIPS16.
mips64
  • MIPS64r6
 

More detailed information about each ABI appears below.

armeabi

This ABI is for ARM-based CPUs that support at least the ARMv5TE instruction set. Please refer to the following documentation for more details:

The AAPCS standard defines EABI as a family of similar but distinct ABIs. Also, Android follows the little-endianARM GNU/Linux ABI.

This ABI does not support hardware-assisted floating point computations. Instead, all floating-point operations use software helper functions from the compiler's libgcc.a static library.

The armeabi ABI supports ARM’s Thumb (a.k.a. Thumb-1) instruction set. The NDK generates Thumb code by default unless you specify different behavior using the LOCAL_ARM_MODE variable in your Android.mk file.

armeabi-v7a (armeabi-v7a-hard)

This ABI extends armeabi to include several CPU instruction set extensions. The instruction extensions that this Android-specific ABI supports are:

  • The Thumb-2 instruction set extension, which provides performance comparable to 32-bit ARM instructions with similar compactness to Thumb-1.
  • The VFP hardware-FPU instructions. More specifically, VFPv3-D16, which includes 16 dedicated 64-bit floating point registers, in addition to another 16 32-bit registers from the ARM core.

Other extensions that the v7-a ARM spec describes, including Advanced SIMD (a.k.a. NEON), VFPv3-D32, and ThumbEE, are optional to this ABI. Since their presence is not guaranteed, the system should check at runtime whether the extensions are available. If they are not, you must use alternative code paths. This check is similar to the one that the system typically performs to check or use MMXSSE2, and other specialized instruction sets on x86 CPUs.

For information about how to perform these runtime checks, refer to The cpufeatures Library. Also, for information about the NDK's support for building machine code for NEON, see NEON Support.

The armeabi-v7a ABI uses the -mfloat-abi=softfp switch to enforce the rule that the compiler must pass all double values in core register pairs during function calls, instead of dedicated floating-point ones. The system can perform all internal computations using the FP registers. Doing so speeds up the computations greatly.

Although the requirement to use core register pairs produces a modest performance hit, it ensures compatibility with all existing armeabi binaries. If you need the additional performance, you can specify your ABI as armeabi-v7a-hard instead. Doing so allows you to use hard floats, while still linking with Android native APIs that usesoftfp. For more information, refer to the comments in $NDK/tests/device/hard-float/jni/android.mk.

Note: You cannot specify APP_ABI as both armeabi-v7a and armeabi-v7a-hard. In either case, the build system places the shared libraries in the armeabi-v7a/ directory.

armeabi-v7a-hard

This variant of the armeabi-v7a ABI is unique to the NDK. The NDK build system adds the following flags in addition to those that it uses for the armeabi-v7a ABI:

TARGET_CFLAGS += -mhard-float -D_NDK_MATH_NO_SOFTFP=1
TARGET_LDFLAGS += -Wl,--no-warn-mismatch -lm_hard

The compiler compiles all code with hard-float, and links it with libm_hard.a. This math library is the same one as libm.a, except that it follows hard-float ABI conventions. In the APK, the generated shared libraries reside in/lib/armeabi-v7a/.

arm64-v8a

This ABI is for ARMv8-based CPUs that support AArch64. It also includes the NEON and VFPv4 instruction sets.

For more information, see the ARMv8 Technology Preview, and contact ARM for further details.

x86

This ABI is for CPUs supporting the instruction set commonly referred to as "x86" or "IA-32". Characteristics of this ABI include:

  • Instructions normally generated by GCC with compiler flags such as the following:
    -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32
    

    These flags target the the Pentium Pro instruction set, along with the the MMXSSESSE2SSE3, and SSSE3instruction set extensions. The generated code is an optimization balanced across the top Intel 32-bit CPUs.

    For more information on compiler flags, particularly related to performance optimization, refer to GCC x86 performance hints.

  • Use of the standard Linux x86 32-bit calling convention, as opposed to the one for SVR. For more information, see section 6, "Register Usage", of Calling conventions for different C++ compilers and operating systems.

The ABI does not include any other optional IA-32 instruction set extensions, such as:

  • MOVBE
  • Any variant of SSE4.

You can still use these extensions, as long as you use runtime feature-probing to enable them, and provide fallbacks for devices that do not support them.

The NDK toolchain assumes 16-byte stack alignment before a function call. The default tools and options enforce this rule. If you are writing assembly code, you must make sure to maintain stack alignment, and ensure that other compilers also obey this rule.

Refer to the following documents for more details:

x86_64

This ABI is for CPUs supporting the instruction set commonly referred to as "x86-64." It supports instructions that GCC typically generates with the following compiler flags:

-march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel

These flags target the x86-64 instruction set, according to the GCC documentation. along with the MMXSSE,SSE2SSE3SSSE3SSE4.1SSE4.2, and POPCNT instruction-set extensions. The generated code is an optimization balanced across the top Intel 64-bit CPUs.

For more information on compiler flags, particularly related to performance optimization, refer to GCC x86 Performance.

This ABI does not include any other optional x86-64 instruction set extensions, such as:

  • MOVBE
  • SHA
  • AVX
  • AVX2

You can still use these extensions, as long as you use runtime feature probing to enable them, and provide fallbacks for devices that do not support them.

Refer to the following documents for more details:

mips

This ABI is for MIPS-based CPUs that support at least the MIPS32r1 instruction set. It includes the following features:

  • MIPS32 revision 1 ISA
  • Little-endian
  • O32
  • Hard-float
  • No DSP application-specific extensions

For more information, please refer to the following documentation:

The MIPS-specific documentation is available here, with further information here.

mips64

This ABI is for MIPS64 R6. For more information, see MIPS64 Architecture.

Generating Code for a Specific ABI


By default, the NDK generates machine code for the armeabi ABI. You can generate ARMv7-a-compatible machine code, instead, by adding the following line to your Application.mk file.

APP_ABI := armeabi-v7a

To build machine code for two or more distinct不同的 ABIs, using spaces as delimiters分隔符. For example:

APP_ABI := armeabi armeabi-v7a

This setting tells the NDK to build two versions of your machine code: one for each ABI listed on this line. For more information on the values you can specify for the APP_ABI variable, see Android.mk.

When you build multiple machine-code versions, the build system copies the libraries to your application project path, and ultimately最终 packages them into your APK, so creating a fat binary. A fat binary is larger than one containing only the machine code for a single system; the tradeoff权衡  is gaining wider compatibility, but at the expense of a larger APK.

At installation time, the package manager unpacks only the most appropriate machine code for the target device. For details, see Automatic extraction of native code at install time.

ABI Management on the Android Platform


This section provides details about how the Android platform manages native code in APKs.

Native code in app packages

Both the Play Store and Package Manager expect to find NDK-generated libraries on filepaths inside the APK matching the following pattern:

/lib/<abi>/lib<name>.so

Here, <abi> is one of the ABI names listed under Supported ABIs, and <name> is the name of the library as you defined it for the LOCAL_MODULE variable in the Android.mk file. Since APK files are just zip files, it is trivial微不足道的 to open them and confirm that the shared native libraries are where they belong.

If the system does not find the native shared libraries where it expects them, it cannot use them. In such a case, the app itself has to copy the libraries over, and then perform dlopen().

In a fat binary, each library resides存在 under a directory whose name matches a corresponding ABI. For example, a fat binary may contain:

/lib/armeabi/libfoo.so
/lib/armeabi-v7a/libfoo.so
/lib/arm64-v8a/libfoo.so
/lib/x86/libfoo.so
/lib/x86_64/libfoo.so
/lib/mips/libfoo.so
/lib/mips64/libfoo.so

Note: ARMv7-based Android devices running 4.0.3 or earlier install native libraries from the armeabidirectory instead of the armeabi-v7a directory if both directories exist. This is because /lib/armeabi/comes after /lib/armeabi-v7a/ in the APK. This issue is fixed from 4.0.4.

Android Platform ABI support

The Android system knows at runtime which ABI(s) it supports, because build-specific system properties indicate指明:

  • The primary主要的 ABI for the device, corresponding相应的 to the machine code used in the system image itself.
  • An optional, secondary ABI, corresponding to another ABI that the system image also supports.

This mechanism机制 ensures that the system extracts the best machine code from the package at installation time.

For best performance, you should compile directly for the primary ABI. For example, a typical ARMv5TE-based device would only define the primary ABI: armeabi. By contrast, a typical, ARMv7-based device would define the primary ABI as armeabi-v7a and the secondary one as armeabi, since it can run application native binaries generated for each of them.

Many x86-based devices can also run armeabi-v7a and armeabi NDK binaries. For such devices, the primary ABI would be x86, and the second one, armeabi-v7a.

A typical MIPS-based device only defines a primary abi: mips.

Automatic extraction提取 of native code at install time

When installing an application, the package manager service scans the APK, and looks for any shared libraries of the form:

lib/<primary-abi>/lib<name>.so

If none is found, and you have defined a secondary ABI, the service scans for shared libraries of the form:

lib/<secondary-abi>/lib<name>.so

When it finds the libraries that it's looking for, the package manager copies them to /lib/lib<name>.so, under the application's data directory (data/data/<package_name>/lib/).

If there is no shared-object file at all, the application builds and installs, but crashes at runtime.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值