Home | History | Annotate | Download | only in guides
      1 page.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.
     17 Each 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
     19 interact with the system at runtime. You must specify an ABI for each CPU architecture you want
     20 your 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
     28 the types of content they support.</li>
     29 <li>Various conventions for passing data between your code and the system.
     30 These conventions include alignment constraints, as well as how the system uses the stack and
     31 registers when it calls functions.</li>
     32 <li>The list of function symbols available to your machine code at runtime,
     33 generally 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
     37 works.</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
     42 the 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
    101 compatibility. 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
    115 the ARMv5TE instruction set. Please refer to the following documentation for
    116 more details:</p>
    117 
    118 <ul>
    119 <li><a href="https://www.scss.tcd.ie/~waldroj/3d1/arm_arm.pdf">ARM Architecture
    120 Reference Manual</a></li>
    121 <li><a
    122 href="http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042e/IHI0042E_aapcs.pdf">
    123 Procedure Call Standard for the ARM Architecture</a></li>
    124 <li><a
    125 href="http://infocenter.arm.com/help/topic/com.arm.doc.dui0101a/DUI0101A_Elf.pdf">
    126 ARM ELF File Format</a></li>
    127 <li><a
    128 href="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
    130 href="http://infocenter.arm.com/help/topic/com.arm.doc.ihi0037c/IHI0037C_bpabi.pdf">
    131 Base Platform ABI for the ARM Architecture</a></li>
    132 <li><a
    133 href="http://infocenter.arm.com/help/topic/com.arm.doc.ihi0039c/IHI0039C_clibabi.pdf">
    134 C Library ABI for the ARM Architecture</a></li>
    135 <li><a
    136 href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ihi0041d/index.html">
    137 C++ ABI for the ARM Architecture</a></li>
    138 <li><a
    139 href="http://infocenter.arm.com/help/topic/com.arm.doc.ihi0043d/IHI0043D_rtabi.pdf">
    140 Run-time ABI for the ARM Architecture</a></li>
    141 <li><a href="http://www.sco.com/developers/gabi/2001-04-24/contents.html">ELF
    142 System V Application Binary Interface</a></li>
    143 <li><a href="http://mentorembedded.github.com/cxx-abi/abi.html">Generic/Itanium C++
    144 ABI</a></li>
    145 </ul>
    146 
    147 <p>The AAPCS standard defines EABI as a family of similar
    148 but 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">
    150 ARM GNU/Linux ABI</a>.</p>
    151 
    152 <p>This ABI does not support hardware-assisted floating point
    153 computations. Instead, all floating-point operations use software helper
    154 functions from the compiler's {@code libgcc.a} static library.</p>
    155 
    156 <p>The armeabi ABI supports ARMs
    157 <a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0210c/CACBCAAE.html">
    158 Thumb (a.k.a. Thumb-1) instruction set</a>. The NDK generates Thumb
    159 code 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>
    162 file.</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">
    167 CPU instruction set extensions</a>. The instruction extensions that this Android-specific
    168 ABI supports are:</p>
    169 
    170 <ul>
    171 <li>The Thumb-2 instruction set extension, which provides performance comparable to 32-bit ARM
    172 instructions with similar compactness to Thumb-1.</li>
    173 <li>The VFP hardware-FPU instructions. More specifically, VFPv3-D16, which
    174 includes 16 dedicated 64-bit floating point registers, in addition to another
    175 16 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">
    180 Advanced SIMD</a> (a.k.a. NEON), VFPv3-D32, and ThumbEE, are optional
    181 to this ABI. Since their presence is not guaranteed, the system should check at runtime
    182 whether the extensions are available. If they are not, you must use alternative code paths. This
    183 check 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
    186 instruction 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>.
    190 Also, for information about the NDK's support for building
    191 machine 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
    195 enforce the rule that the compiler must pass all double values in core register pairs during
    196 function calls, instead of dedicated floating-point ones. The system can perform all internal
    197 computations 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
    201 VFPv4 instruction sets.</p>
    202 
    203 <p>For more information, see the
    204 <a href="http://www.arm.com/files/downloads/ARMv8_Architecture.pdf">ARMv8
    205 Technology 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
    209 referred 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
    219 the <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.
    224 The generated code is an optimization balanced across the top Intel 32-bit
    225 CPUs.</p>
    226 <p> For more information on compiler flags, particularly related to performance optimization,
    227 refer to <a href="http://software.intel.com/blogs/2012/09/26/gcc-x86-performance-hints">GCC
    228 x86 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
    231 more information, see section 6, "Register Usage", of
    232 <a href="http://www.agner.org/optimize/calling_conventions.pdf">Calling conventions for different
    233 C++ compilers and operating systems</a>.</li>
    234 </ul>
    235 
    236 <p>The ABI does not include any other optional IA-32 instruction set
    237 extensions, 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
    243 enable 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
    245 options enforce this rule. If you are writing assembly code, you must make sure to maintain stack
    246 alignment, 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">
    252 GCC online documentation: Intel 386 and AMD x86-64 Options</a></li>
    253 <li><a href="http://www.agner.org/optimize/calling_conventions.pdf">Calling
    254 conventions for different C++ compilers and operating systems</a></li>
    255 <li><a
    256 href="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:
    258 Instruction Set Reference</a></li>
    259 <li><a
    260 href="http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-system-programming-manual-325384.pdf">Intel
    261 IA-32 Intel Architecture Software Developer's Manual, Volume 3: System
    262 Programming Guide</a></li>
    263 <li><a href="http://www.sco.com/developers/devspecs/abi386-4.pdf">System V Application Binary
    264 Interface: 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
    270 compiler 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
    276 documentation. 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>
    285 instruction-set extensions. The generated code is an optimization balanced
    286 across the top Intel 64-bit CPUs.</p>
    287 
    288 <p> For more information on compiler flags, particularly related to performance optimization,
    289 refer to <a href="http://software.intel.com/blogs/2012/09/26/gcc-x86-performance-hints">GCC
    290 x86 Performance</a>.</p>
    291 
    292 <p>This ABI does not include any other optional x86-64 instruction set
    293 extensions, 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
    303 enable 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
    308 different 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">
    311 Intel64 and IA-32 Architectures Software Developer's Manual, Volume 2: Instruction Set
    312 Reference</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">
    315 Intel64 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
    321 the 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
    336 System V Application Binary Interface</a></li>
    337 <li><a href="http://sourcery.mentor.com/public/cxx-abi/abi.html">Itanium/Generic C++
    338 ABI</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>.
    343 Answers 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
    355 generate ARMv7-a-compatible machine code, instead, by adding the following line
    356 to your <a href="{@docRoot}ndk/guides/application_mk.html">{@code Application.mk}</a> file.</p>
    357 <pre class="no-pretty-print">
    358 APP_ABI := armeabi-v7a
    359 </pre>
    360 
    361 <p>To build machine code for two or more distinct ABIs, using spaces as delimiters. For
    362 example:</p>
    363 
    364 <pre class="no-pretty-print">
    365 APP_ABI := armeabi armeabi-v7a
    366 </pre>
    367 
    368 <p>This setting tells the NDK to build two versions of your machine code: one
    369 for 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
    374 application project path, and ultimately packages them into your APK, so creating
    375 a <a href="http://en.wikipedia.org/wiki/Fat_binary"><i>fat binary</i></a>. A fat binary
    376 is larger than one containing only the machine code for a single system; the tradeoff is
    377 gaining 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
    380 machine code for the target device. For details, see <a href="#aen">Automatic
    381 extraction 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
    386 code 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
    390 libraries on filepaths inside the APK matching the following pattern:</p>
    391 
    392 <pre class="no-pretty-print">
    393 /lib/&lt;abi&gt;/lib&lt;name&gt;.so
    394 </pre>
    395 
    396 <p>Here, {@code <abi>} is one of the ABI names listed under <a href="#sa">Supported ABIs</a>,
    397 and {@code <name>} is the name of the library as you defined it for the {@code LOCAL_MODULE}
    398 variable in the <a href="{@docRoot}ndk/guides/android_mk.html">{@code Android.mk}</a> file. Since
    399 APK files are just zip files, it is trivial to open them and confirm that the shared native
    400 libraries 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
    403 them. In such a case, the app itself has to copy the libraries over, and then
    404 perform <code>dlopen()</code>.</p>
    405 
    406 <p>In a fat binary, each library resides under a directory whose name matches a corresponding ABI.
    407 For 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
    420 install native libraries from the {@code armeabi} directory instead of the {@code armeabi-v7a}
    421 directory 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
    426 properties indicate:</p>
    427 
    428 <ul>
    429 <li>The primary ABI for the device, corresponding to the machine code used in
    430 the 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
    436 the package at installation time.</p>
    437 
    438 <p>For best performance, you should compile directly for the primary ABI. For example, a
    439 typical ARMv5TE-based device would only define the primary ABI: {@code armeabi}. By contrast, a
    440 typical, ARMv7-based device would define the primary ABI as {@code armeabi-v7a} and the secondary
    441 one 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
    444 such 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
    451 shared libraries of the form:</p>
    452 
    453 <pre class="no-pretty-print">
    454 lib/&lt;primary-abi&gt;/lib&lt;name&gt;.so
    455 </pre>
    456 
    457 <p>If none is found, and you have defined a secondary ABI, the service scans for shared libraries of
    458 the form:</p>
    459 
    460 <pre class="no-pretty-print">
    461 lib/&lt;secondary-abi&gt;/lib&lt;name&gt;.so
    462 </pre>
    463 
    464 <p>When it finds the libraries that it's looking for, the package manager
    465 copies them to <code>/lib/lib&lt;name&gt;.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
    469 runtime.</p>
    470