1page.title=ABI Management 2@jd:body 3 4<div id="qv-wrapper"> 5 <div id="qv"> 6 <h2>On this page</h2> 7 8 <ol> 9 <li><a href="#sa">Supported ABIs</a></li> 10 <li><a href="#gc">Generating Code for a Specific ABI</a></li> 11 <li><a href="#am">ABI Management on the Android Platform</a></li> 12 </ol> 13 </div> 14 </div> 15 16<p>Different Android handsets use different CPUs, which in turn support different instruction sets. 17Each combination of CPU and instruction sets has its own Application Binary Interface, or 18<i>ABI</i>. The ABI defines, with great precision, how an application's machine code is supposed to 19interact with the system at runtime. You must specify an ABI for each CPU architecture you want 20your app to work with.</p> 21 22<p>A typical ABI includes the following information:</p> 23 24<ul> 25<li>The CPU instruction set(s) that the machine code should use.</li> 26<li>The endianness of memory stores and loads at runtime.</li> 27<li>The format of executable binaries, such as programs and shared libraries, and 28the types of content they support.</li> 29<li>Various conventions for passing data between your code and the system. 30These conventions include alignment constraints, as well as how the system uses the stack and 31registers when it calls functions.</li> 32<li>The list of function symbols available to your machine code at runtime, 33generally from very specific sets of libraries.</li> 34</ul> 35 36<p>This page enumerates the ABIs that the NDK supports, and provides information about how each ABI 37works.</p> 38 39<h2 id="sa">Supported ABIs</h2> 40 41<p>Each ABI supports one or more instruction sets. Table 1 provides an at-a-glance overview of 42the instruction sets each ABI supports.</p> 43 44<p class="table-caption" id="abi-table"> 45 <strong>Table 1.</strong> ABIs and supported instruction sets.</p> 46 47<table> 48<tr> 49<th>ABI</th> 50<th>Supported Instruction Set(s)</th> 51<th>Notes</th> 52</tr> 53 54<tr> 55<td><a href="#armeabi">{@code armeabi}</a> </td> 56<td><li>ARMV5TE and later</li> 57<li>Thumb-1</li></td> 58<td>No hard float.</td> 59</tr> 60 61<tr> 62<td><a href="#v7a">{@code armeabi-v7a}</a></td> 63<td> 64<li>armeabi</li> 65<li>Thumb-2</li> 66<li>VFPv3-D16</li> 67<li>Other, optional</li></td> 68<td>Incompatible with ARMv5, v6 devices.</td> 69</tr> 70 71<tr> 72<td><a href="#arm64-v8a">{@code arm64-v8a}</a></td> 73<td><li>AArch-64</li></td> 74</tr> 75 76<tr> 77<td> 78<a href="#x86">{@code x86}</a></td> 79<td><li>x86 (IA-32)</li> 80<li>MMX</li> 81<li>SSE/2/3</li> 82<li>SSSE3</li></td> 83<td>No support for MOVBE or SSE4.</td> 84</tr> 85 86<tr> 87<td><a href="#86-64">{@code x86_64}</a> </td> 88<td> 89<li>x86-64</li> 90<li>MMX</li> 91<li>SSE/2/3</li> 92<li>SSSE3</li> 93<li>SSE4.1, 4.2</li> 94<li>POPCNT</li></td> 95</tr> 96 97<tr> 98<td><a href="#mips">{@code mips}</a></td> 99<td><li>MIPS32r1 and later</li></td> 100<td>Uses hard-float, and assumes a CPU:FPU clock ratio of 2:1 for maximum 101compatibility. Provides neither micromips nor MIPS16.</td> 102</tr> 103 104<tr> 105<td><a href="#mips64">{@code mips64}</a></td> 106<td><li>MIPS64r6</li></td><td> 107</td> 108</tr> 109</table> 110 111<p>More detailed information about each ABI appears below.</p> 112 113<h3 id="armeabi">armeabi</h3> 114<p>This ABI is for ARM-based CPUs that support at least 115the ARMv5TE instruction set. Please refer to the following documentation for 116more details:</p> 117 118<ul> 119<li><a href="https://www.scss.tcd.ie/~waldroj/3d1/arm_arm.pdf">ARM Architecture 120Reference Manual</a></li> 121<li><a 122href="http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042e/IHI0042E_aapcs.pdf"> 123Procedure Call Standard for the ARM Architecture</a></li> 124<li><a 125href="http://infocenter.arm.com/help/topic/com.arm.doc.dui0101a/DUI0101A_Elf.pdf"> 126ARM ELF File Format</a></li> 127<li><a 128href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.subset.swdev.abi/index.html">Application Binary Interface (ABI) for the ARM Architecture</a></li> 129<li><a 130href="http://infocenter.arm.com/help/topic/com.arm.doc.ihi0037c/IHI0037C_bpabi.pdf"> 131Base Platform ABI for the ARM Architecture</a></li> 132<li><a 133href="http://infocenter.arm.com/help/topic/com.arm.doc.ihi0039c/IHI0039C_clibabi.pdf"> 134C Library ABI for the ARM Architecture</a></li> 135<li><a 136href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ihi0041d/index.html"> 137C++ ABI for the ARM Architecture</a></li> 138<li><a 139href="http://infocenter.arm.com/help/topic/com.arm.doc.ihi0043d/IHI0043D_rtabi.pdf"> 140Run-time ABI for the ARM Architecture</a></li> 141<li><a href="http://www.sco.com/developers/gabi/2001-04-24/contents.html">ELF 142System V Application Binary Interface</a></li> 143<li><a href="http://mentorembedded.github.com/cxx-abi/abi.html">Generic/Itanium C++ 144ABI</a></li> 145</ul> 146 147<p>The AAPCS standard defines EABI as a family of similar 148but distinct ABIs. Also, Android follows the little-endian 149<a href="http://sourcery.mentor.com/sgpp/lite/arm/portal/kbattach142/arm_gnu_linux_ abi.pdf"> 150ARM GNU/Linux ABI</a>.</p> 151 152<p>This ABI does not support hardware-assisted floating point 153computations. Instead, all floating-point operations use software helper 154functions from the compiler's {@code libgcc.a} static library.</p> 155 156<p>The armeabi ABI supports ARM’s 157<a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0210c/CACBCAAE.html"> 158Thumb (a.k.a. Thumb-1) instruction set</a>. The NDK generates Thumb 159code by default unless you specify different behavior using the 160<code>LOCAL_ARM_MODE</code> variable in your 161<a href="{@docRoot}ndk/guides/android_mk.html">{@code Android.mk}</a> 162file.</p> 163 164<h3 id="v7a">armeabi-v7a</h3> 165<p>This ABI extends armeabi to include several 166<a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0406c/index.html"> 167CPU instruction set extensions</a>. The instruction extensions that this Android-specific 168ABI supports are:</p> 169 170<ul> 171<li>The Thumb-2 instruction set extension, which provides performance comparable to 32-bit ARM 172instructions with similar compactness to Thumb-1.</li> 173<li>The VFP hardware-FPU instructions. More specifically, VFPv3-D16, which 174includes 16 dedicated 64-bit floating point registers, in addition to another 17516 32-bit registers from the ARM core.</li> 176</ul> 177 178<p>Other extensions that the v7-a ARM spec describes, including 179<a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0388f/Beijfcja.html"> 180Advanced SIMD</a> (a.k.a. NEON), VFPv3-D32, and ThumbEE, are optional 181to this ABI. Since their presence is not guaranteed, the system should check at runtime 182whether the extensions are available. If they are not, you must use alternative code paths. This 183check is similar to the one that the system typically performs to check or use 184<a href="http://en.wikipedia.org/wiki/MMX_%28instruction_set%29">MMX</a>, 185<a href="http://en.wikipedia.org/wiki/SSE2">SSE2</a>, and other specialized 186instruction sets on x86 CPUs.</p> 187 188<p>For information about how to perform these runtime checks, refer to 189<a href="{@docRoot}ndk/guides/cpu-features.html">The {@code cpufeatures} Library</a>. 190Also, for information about the NDK's support for building 191machine code for NEON, see 192<a href="{@docRoot}ndk/guides/cpu-arm-neon.html">NEON Support</a>.</p> 193 194<p>The {@code armeabi-v7a} ABI uses the {@code -mfloat-abi=softfp} switch to 195enforce the rule that the compiler must pass all double values in core register pairs during 196function calls, instead of dedicated floating-point ones. The system can perform all internal 197computations using the FP registers. Doing so speeds up the computations greatly.</p> 198 199<h3 id="arm64-v8a">arm64-v8a</h3> 200<p>This ABI is for ARMv8-based CPUs that support AArch64. It also includes the NEON and 201VFPv4 instruction sets.</p> 202 203<p>For more information, see the 204<a href="http://www.arm.com/files/downloads/ARMv8_Architecture.pdf">ARMv8 205Technology Preview</a>, and contact ARM for further details.</p> 206 207<h3 id="x86">x86</h3> 208<p>This ABI is for CPUs supporting the instruction set commonly 209referred to as "x86" or "IA-32". Characteristics of this ABI include:</p> 210 211<ul> 212<li>Instructions normally generated by GCC with compiler flags such as the following: 213 214<pre class="no-pretty-print"> 215-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32 216</pre> 217 218<p>These flags target the the Pentium Pro instruction set, along with the 219the <a href="http://en.wikipedia.org/wiki/MMX_%28instruction_set%29">MMX</a>, 220<a href="http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions">SSE</a>, 221<a href="http://en.wikipedia.org/wiki/SSE2">SSE2</a>, 222<a href="http://en.wikipedia.org/wiki/SSE3">SSE3</a>, and 223<a href="http://en.wikipedia.org/wiki/SSSE3">SSSE3</a> instruction set extensions. 224The generated code is an optimization balanced across the top Intel 32-bit 225CPUs.</p> 226<p> For more information on compiler flags, particularly related to performance optimization, 227refer to <a href="http://software.intel.com/blogs/2012/09/26/gcc-x86-performance-hints">GCC 228x86 performance hints</a>.</p> 229</li> 230<li>Use of the standard Linux x86 32-bit calling convention, as opposed to the one for SVR. For 231more information, see section 6, "Register Usage", of 232<a href="http://www.agner.org/optimize/calling_conventions.pdf">Calling conventions for different 233C++ compilers and operating systems</a>.</li> 234</ul> 235 236<p>The ABI does not include any other optional IA-32 instruction set 237extensions, such as:</p> 238<ul> 239<li>MOVBE</li> 240<li>Any variant of SSE4.</li> 241</ul> 242<p>You can still use these extensions, as long as you use runtime feature-probing to 243enable them, and provide fallbacks for devices that do not support them.</p> 244<p>The NDK toolchain assumes 16-byte stack alignment before a function call. The default tools and 245options enforce this rule. If you are writing assembly code, you must make sure to maintain stack 246alignment, and ensure that other compilers also obey this rule.</p> 247 248<p>Refer to the following documents for more details:</p> 249<ul> 250<li> 251<a href="https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/i386-and-x86-64-Options.html"> 252GCC online documentation: Intel 386 and AMD x86-64 Options</a></li> 253<li><a href="http://www.agner.org/optimize/calling_conventions.pdf">Calling 254conventions for different C++ compilers and operating systems</a></li> 255<li><a 256href="http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf" 257>Intel IA-32 Intel Architecture Software Developer's Manual, Volume 2: 258Instruction Set Reference</a></li> 259<li><a 260href="http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-system-programming-manual-325384.pdf">Intel 261IA-32 Intel Architecture Software Developer's Manual, Volume 3: System 262Programming Guide</a></li> 263<li><a href="http://www.sco.com/developers/devspecs/abi386-4.pdf">System V Application Binary 264Interface: Intel386 Processor Architecture Supplement</a></li> 265</ul> 266 267<h3 id="86-64">x86_64</h3> 268<p>This ABI is for CPUs supporting the instruction set commonly referred to as 269"x86-64." It supports instructions that GCC typically generates with the following 270compiler flags:</p> 271<pre class="no-pretty-print"> 272-march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel 273</pre> 274 275<p>These flags target the x86-64 instruction set, according to the GCC 276documentation. along with the 277<a href="http://en.wikipedia.org/wiki/MMX_%28instruction_set%29">MMX</a>, 278<a href="http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions">SSE</a>, 279<a href="http://en.wikipedia.org/wiki/SSE2">SSE2</a>, 280<a href="http://en.wikipedia.org/wiki/SSE3">SSE3</a>, 281<a href="http://en.wikipedia.org/wiki/SSSE3">SSSE3</a>, 282<a href="http://en.wikipedia.org/wiki/SSE4#SSE4.1">SSE4.1</a>, 283<a href="http://en.wikipedia.org/wiki/SSE4#SSE4.2">SSE4.2</a>, and 284<a href="https://software.intel.com/en-us/node/512035">POPCNT</a> 285instruction-set extensions. The generated code is an optimization balanced 286across the top Intel 64-bit CPUs.</p> 287 288<p> For more information on compiler flags, particularly related to performance optimization, 289refer to <a href="http://software.intel.com/blogs/2012/09/26/gcc-x86-performance-hints">GCC 290x86 Performance</a>.</p> 291 292<p>This ABI does not include any other optional x86-64 instruction set 293extensions, such as:</p> 294 295<ul> 296<li>MOVBE</li> 297<li>SHA</li> 298<li>AVX</li> 299<li>AVX2</li> 300</ul> 301 302<p>You can still use these extensions, as long as you use runtime feature probing to 303enable them, and provide fallbacks for devices that do not support them.</p> 304<p>Refer to the following documents for more details:</p> 305 306<ul> 307<li><a href="http://www.agner.org/optimize/calling_conventions.pdf">Calling conventions for 308different C++ compilers and operating systems</a></li> 309<li> 310<a href="http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html?iid=tech_vt_tech+64-32_manuals"> 311Intel64 and IA-32 Architectures Software Developer's Manual, Volume 2: Instruction Set 312Reference</a></li> 313<li> 314<a href="http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html?iid=tech_vt_tech+64-32_manuals"> 315Intel64 and IA-32 Intel Architecture Software Developer's Manual Volume 3: System Programming</a> 316</li> 317</ul> 318 319<h3 id="mips">mips</h3> 320<p>This ABI is for MIPS-based CPUs that support at least the MIPS32r1 instruction set. It includes 321the following features:</p> 322 323<ul> 324<li>MIPS32 revision 1 ISA</li> 325<li>Little-endian</li> 326<li>O32</li> 327<li>Hard-float</li> 328<li>No DSP application-specific extensions</li> 329</ul> 330 331<p>For more information, please refer to the following documentation:</p> 332 333<ul> 334<li>Architecture for Programmers ("MIPSARCH")</li> 335<li><a href="https://refspecs.linuxbase.org/elf/gabi4+/contents.html">ELF 336System V Application Binary Interface</a></li> 337<li><a href="http://sourcery.mentor.com/public/cxx-abi/abi.html">Itanium/Generic C++ 338ABI</a></li> 339</ul> 340 341<p>For more specific details, see 342<a href="http://www.imgtec.com/mips/architectures/mips32/">MIPS32 Architecture</a>. 343Answers to common questions are in the 344<a href="https://sourcery.mentor.com/sgpp/lite/mips/portal/target_arch?@action=faq&target_arch=MIPS">MIPS FAQ</a>. 345</p> 346</li> 347</ul> 348 349<h3 id="mips64">mips64</h3> 350<p>This ABI is for MIPS64 R6. For more information, see 351<a href="http://www.imgtec.com/mips/architectures/mips64/">MIPS64 Architecture</a>.</p> 352 353<h2 id="gc">Generating Code for a Specific ABI</h2> 354<p>By default, the NDK generates machine code for the armeabi ABI. You can 355generate ARMv7-a-compatible machine code, instead, by adding the following line 356to your <a href="{@docRoot}ndk/guides/application_mk.html">{@code Application.mk}</a> file.</p> 357<pre class="no-pretty-print"> 358APP_ABI := armeabi-v7a 359</pre> 360 361<p>To build machine code for two or more distinct ABIs, using spaces as delimiters. For 362example:</p> 363 364<pre class="no-pretty-print"> 365APP_ABI := armeabi armeabi-v7a 366</pre> 367 368<p>This setting tells the NDK to build two versions of your machine code: one 369for each ABI listed on this line. For more information on the values you can specify for the 370{@code APP_ABI} variable, see <a href="{@docRoot}ndk/guides/android_mk.html">Android.mk</a>. 371</p> 372 373<p>When you build multiple machine-code versions, the build system copies the libraries to your 374application project path, and ultimately packages them into your APK, so creating 375a <a href="http://en.wikipedia.org/wiki/Fat_binary"><i>fat binary</i></a>. A fat binary 376is larger than one containing only the machine code for a single system; the tradeoff is 377gaining wider compatibility, but at the expense of a larger APK.</p> 378 379<p>At installation time, the package manager unpacks only the most appropriate 380machine code for the target device. For details, see <a href="#aen">Automatic 381extraction of native code at install time</a>.</p> 382 383 384<h2 id="am">ABI Management on the Android Platform</h2> 385<p>This section provides details about how the Android platform manages native 386code in APKs.</p> 387 388<h3>Native code in app packages</h3> 389<p>Both the Play Store and Package Manager expect to find NDK-generated 390libraries on filepaths inside the APK matching the following pattern:</p> 391 392<pre class="no-pretty-print"> 393/lib/<abi>/lib<name>.so 394</pre> 395 396<p>Here, {@code <abi>} is one of the ABI names listed under <a href="#sa">Supported ABIs</a>, 397and {@code <name>} is the name of the library as you defined it for the {@code LOCAL_MODULE} 398variable in the <a href="{@docRoot}ndk/guides/android_mk.html">{@code Android.mk}</a> file. Since 399APK files are just zip files, it is trivial to open them and confirm that the shared native 400libraries are where they belong.</p> 401 402<p>If the system does not find the native shared libraries where it expects them, it cannot use 403them. In such a case, the app itself has to copy the libraries over, and then 404perform <code>dlopen()</code>.</p> 405 406<p>In a fat binary, each library resides under a directory whose name matches a corresponding ABI. 407For example, a fat binary may contain:</p> 408 409<pre class="no-pretty-print"> 410/lib/armeabi/libfoo.so 411/lib/armeabi-v7a/libfoo.so 412/lib/arm64-v8a/libfoo.so 413/lib/x86/libfoo.so 414/lib/x86_64/libfoo.so 415/lib/mips/libfoo.so 416/lib/mips64/libfoo.so 417</pre> 418 419<p class="note"><strong>Note:</strong> ARMv7-based Android devices running 4.0.3 or earlier 420install native libraries from the {@code armeabi} directory instead of the {@code armeabi-v7a} 421directory if both directories exist. This is because {@code /lib/armeabi/} comes after 422{@code /lib/armeabi-v7a/} in the APK. This issue is fixed from 4.0.4.</p> 423 424<h3>Android Platform ABI support</h3> 425<p>The Android system knows at runtime which ABI(s) it supports, because build-specific system 426properties indicate:</p> 427 428<ul> 429<li>The primary ABI for the device, corresponding to the machine code used in 430the system image itself.</li> 431<li>An optional, secondary ABI, corresponding to another ABI that the system image also supports. 432</li> 433</ul> 434 435<p>This mechanism ensures that the system extracts the best machine code from 436the package at installation time.</p> 437 438<p>For best performance, you should compile directly for the primary ABI. For example, a 439typical ARMv5TE-based device would only define the primary ABI: {@code armeabi}. By contrast, a 440typical, ARMv7-based device would define the primary ABI as {@code armeabi-v7a} and the secondary 441one as {@code armeabi}, since it can run application native binaries generated for each of them.</p> 442 443<p>Many x86-based devices can also run {@code armeabi-v7a} and {@code armeabi} NDK binaries. For 444such devices, the primary ABI would be {@code x86}, and the second one, {@code armeabi-v7a}.</p> 445 446<p>A typical MIPS-based device only defines a primary abi: {@code mips}.</p> 447 448<h3 id="aen">Automatic extraction of native code at install time</h3> 449 450<p>When installing an application, the package manager service scans the APK, and looks for any 451shared libraries of the form:</p> 452 453<pre class="no-pretty-print"> 454lib/<primary-abi>/lib<name>.so 455</pre> 456 457<p>If none is found, and you have defined a secondary ABI, the service scans for shared libraries of 458the form:</p> 459 460<pre class="no-pretty-print"> 461lib/<secondary-abi>/lib<name>.so 462</pre> 463 464<p>When it finds the libraries that it's looking for, the package manager 465copies them to <code>/lib/lib<name>.so</code>, under the application's 466{@code data} directory ({@code data/data/<package_name>/lib/}).</p> 467 468<p>If there is no shared-object file at all, the application builds and installs, but crashes at 469runtime.</p> 470