Elliott Hughes | 8f3f191 | 2019-08-15 08:19:49 -0700 | [diff] [blame] | 1 | # Android linker changes for NDK developers |
Elliott Hughes | c45380d | 2016-10-05 14:35:00 -0700 | [diff] [blame] | 2 | |
| 3 | This document details important changes related to native code |
| 4 | loading in various Android releases. |
| 5 | |
Elliott Hughes | 8f3f191 | 2019-08-15 08:19:49 -0700 | [diff] [blame] | 6 | See also [bionic status](docs/status.md) for general libc/libm/libdl |
| 7 | behavior changes. |
| 8 | |
Elliott Hughes | c45380d | 2016-10-05 14:35:00 -0700 | [diff] [blame] | 9 | Required tools: the NDK has an _arch_-linux-android-readelf binary |
| 10 | (e.g. arm-linux-androideabi-readelf or i686-linux-android-readelf) |
| 11 | for each architecture (under toolchains/), but you can use readelf for |
| 12 | any architecture, as we will be doing basic inspection only. On Linux |
| 13 | you need to have the “binutils” package installed for readelf, |
| 14 | and “pax-utils” for scanelf. |
| 15 | |
| 16 | |
Elliott Hughes | 9e3d527 | 2017-01-13 11:07:00 -0800 | [diff] [blame] | 17 | ## How we manage incompatible changes |
| 18 | |
| 19 | Our general practice with dynamic linker behavior changes is that they |
| 20 | will be tied to an app's target API level: |
| 21 | |
| 22 | * Below the affected API level we'll preserve the old behavior or issue |
| 23 | a warning, as appropriate. |
| 24 | |
| 25 | * At the affected API level and above, we’ll refuse to load the library. |
| 26 | |
| 27 | * Warnings about any behavior change that will affect a library if you |
| 28 | increase your target API level will appear in logcat when that library |
| 29 | is loaded, even if you're not yet targeting that API level. |
| 30 | |
| 31 | * On a developer preview build, dynamic linker warnings will also show up |
| 32 | as toasts. Experience has shown that many developers don’t habitually |
| 33 | check logcat for warnings until their app stops functioning, so the |
| 34 | toasts help bring some visibility to the issues before it's too late. |
| 35 | |
Elliott Hughes | dc66073 | 2018-05-01 11:27:46 -0700 | [diff] [blame] | 36 | ## Changes to library dependency resolution |
| 37 | |
| 38 | Until it was [fixed](https://issuetracker.google.com/36950617) in |
| 39 | JB-MR2, Android didn't include the application library directory |
| 40 | on the dynamic linker's search path. This meant that apps |
| 41 | had to call `dlopen` or `System.loadLibrary` on all transitive |
| 42 | dependencies before loading their main library. Worse, until it was |
| 43 | [fixed](https://issuetracker.google.com/36935779) in JB-MR2, the |
| 44 | dynamic linker's caching code cached failures too, so it was necessary |
| 45 | to topologically sort your libraries and load them in reverse order. |
| 46 | |
| 47 | If you need to support Android devices running OS |
| 48 | versions older than JB-MR2, you might want to consider |
| 49 | [ReLinker](https://github.com/KeepSafe/ReLinker) which claims to solve |
| 50 | these problems automatically. |
Elliott Hughes | 9e3d527 | 2017-01-13 11:07:00 -0800 | [diff] [blame] | 51 | |
Elliott Hughes | 3230b68 | 2019-08-05 16:40:52 -0700 | [diff] [blame] | 52 | Alternatively, if you don't have too many dependencies, it can be easiest to |
| 53 | simply link all of your code into one big library and sidestep the details of |
| 54 | library and symbol lookup changes on all past (and future) Android versions. |
| 55 | |
Elliott Hughes | 1777aaf | 2016-11-18 12:28:01 -0800 | [diff] [blame] | 56 | ## Changes to library search order |
| 57 | |
| 58 | We have made various fixes to library search order when resolving symbols. |
| 59 | |
| 60 | With API 22, load order switched from depth-first to breadth-first to |
| 61 | fix dlsym(3). |
| 62 | |
| 63 | Before API 23, the default search order was to try the main executable, |
| 64 | LD_PRELOAD libraries, the library itself, and its DT_NEEDED libraries |
| 65 | in that order. For API 23 and later, for any given library, the dynamic |
| 66 | linker divides other libraries into the global group and the local |
| 67 | group. The global group is shared by all libraries and contains the main |
| 68 | executable, LD_PRELOAD libraries, and any library with the DF_1_GLOBAL |
| 69 | flag set (by passing “-z global” to ld(1)). The local group is |
| 70 | the breadth-first transitive closure of the library and its DT_NEEDED |
| 71 | libraries. The M dynamic linker searches the global group followed by |
| 72 | the local group. This allows ASAN, for example, to ensure that it can |
| 73 | intercept any symbol. |
| 74 | |
| 75 | |
Elliott Hughes | 8e4b6b6 | 2019-06-05 08:28:55 -0700 | [diff] [blame] | 76 | ## LD_PRELOAD and 32/64 bit |
| 77 | |
| 78 | LD_PRELOAD applies to both 32- and 64-bit processes. This means that you |
| 79 | should avoid saying something like `/system/lib/libfoo.so` and just say |
| 80 | `libfoo.so` instead, letting the dynamic linker find the correct library |
| 81 | on its search path. |
| 82 | |
| 83 | |
Elliott Hughes | 1777aaf | 2016-11-18 12:28:01 -0800 | [diff] [blame] | 84 | ## RTLD_LOCAL (Available in API level >= 23) |
| 85 | |
| 86 | The dlopen(3) RTLD_LOCAL flag used to be ignored but is implemented |
| 87 | correctly in API 23 and later. Note that RTLD_LOCAL is the default, |
| 88 | so even calls to dlopen(3) that didn’t explicitly use RTLD_LOCAL will |
| 89 | be affected (unless they explicitly used RTLD_GLOBAL). With RTLD_LOCAL, |
| 90 | symbols will not be made available to libraries loaded by later calls |
| 91 | to dlopen(3) (as opposed to being referenced by DT_NEEDED entries). |
| 92 | |
| 93 | |
| 94 | ## GNU hashes (Availible in API level >= 23) |
| 95 | |
| 96 | The GNU hash style available with --hash-style=gnu allows faster |
| 97 | symbol lookup and is now supported by the dynamic linker in API 23 and |
| 98 | above. (Use --hash-style=both if you want to build code that uses this |
| 99 | feature >= Android M but still works on older releases.) |
| 100 | |
| 101 | |
| 102 | ## Correct soname/path handling (Available in API level >= 23) |
| 103 | |
| 104 | The dynamic linker now understands the difference |
| 105 | between a library’s soname and its path (public bug |
| 106 | https://code.google.com/p/android/issues/detail?id=6670). API level 23 |
| 107 | is the first release where search by soname is implemented. Earlier |
| 108 | releases would assume that the basename of the library was the soname, |
| 109 | and used that to search for already-loaded libraries. For example, |
| 110 | `dlopen("/this/directory/does/not/exist/libc.so", RTLD_NOW)` would |
| 111 | find `/system/lib/libc.so` because it’s already loaded. This also meant |
| 112 | that it was impossible to have two libraries `"dir1/libx.so"` and |
| 113 | `"dir2/libx.so"` --- the dynamic linker couldn’t tell the difference |
| 114 | and would always use whichever was loaded first, even if you explicitly |
| 115 | tried to load both. This also applied to DT_NEEDED entries. |
| 116 | |
| 117 | Some apps have bad DT_NEEDED entries (usually absolute paths on the build |
| 118 | machine’s file system) that used to work because we ignored everything |
| 119 | but the basename. These apps will fail to load on API level 23 and above. |
| 120 | |
| 121 | |
| 122 | ## Symbol versioning (Available in API level >= 23) |
| 123 | |
| 124 | Symbol versioning allows libraries to provide better backwards |
| 125 | compatibility. For example, if a library author knowingly changes |
| 126 | the behavior of a function, they can provide two versions in the same |
| 127 | library so that old code gets the old version and new code gets the new |
| 128 | version. This is supported in API level 23 and above. |
| 129 | |
| 130 | |
| 131 | ## Opening shared libraries directly from an APK |
| 132 | |
| 133 | In API level 23 and above, it’s possible to open a .so file directly from |
| 134 | your APK. Just use `System.loadLibrary("foo")` exactly as normal but set |
| 135 | `android:extractNativeLibs="false"` in your `AndroidManifest.xml`. In |
| 136 | older releases, the .so files were extracted from the APK file |
| 137 | at install time. This meant that they took up space in your APK and |
| 138 | again in your installation directory (and this was counted against you |
| 139 | and reported to the user as space taken up by your app). Any .so file |
| 140 | that you want to load directly from your APK must be page aligned |
| 141 | (on a 4096-byte boundary) in the zip file and stored uncompressed. |
| 142 | Current versions of the zipalign tool take care of alignment. |
| 143 | |
| 144 | Note that in API level 23 and above dlopen(3) will open a library from |
| 145 | any zip file, not just your APK. Just give dlopen(3) a path of the form |
| 146 | "my_zip_file.zip!/libs/libstuff.so". As with APKs, the library must be |
| 147 | page-aligned and stored uncompressed for this to work. |
| 148 | |
| 149 | |
Elliott Hughes | c45380d | 2016-10-05 14:35:00 -0700 | [diff] [blame] | 150 | ## Private API (Enforced for API level >= 24) |
| 151 | |
| 152 | Native libraries must use only public API, and must not link against |
| 153 | non-NDK platform libraries. Starting with API 24 this rule is enforced and |
| 154 | applications are no longer able to load non-NDK platform libraries. The |
| 155 | rule is enforced by the dynamic linker, so non-public libraries |
| 156 | are not accessible regardless of the way code tries to load them: |
| 157 | System.loadLibrary, DT_NEEDED entries, and direct calls to dlopen(3) |
| 158 | will all work exactly the same. |
| 159 | |
| 160 | Users should have a consistent app experience across updates, |
| 161 | and developers shouldn't have to make emergency app updates to |
| 162 | handle platform changes. For that reason, we recommend against using |
| 163 | private C/C++ symbols. Private symbols aren't tested as part of the |
| 164 | Compatibility Test Suite (CTS) that all Android devices must pass. They |
| 165 | may not exist, or they may behave differently. This makes apps that use |
| 166 | them more likely to fail on specific devices, or on future releases --- |
| 167 | as many developers found when Android 6.0 Marshmallow switched from |
| 168 | OpenSSL to BoringSSL. |
| 169 | |
| 170 | In order to reduce the user impact of this transition, we've identified |
| 171 | a set of libraries that see significant use from Google Play's |
| 172 | most-installed apps, and that are feasible for us to support in the |
| 173 | short term (including libandroid_runtime.so, libcutils.so, libcrypto.so, |
| 174 | and libssl.so). In order to give you more time to transition, we will |
| 175 | temporarily support these libraries; so if you see a warning that means |
| 176 | your code will not work in a future release -- please fix it now! |
| 177 | |
Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 178 | In O and later, the system property `debug.ld.greylist_disabled` can be |
| 179 | used to deny access to the greylist even to an app that would normally |
| 180 | be allowed it. This allows you to test compatibility without bumping the |
| 181 | app's `targetSdkVersion`. Use `setprop debug.ld.greylist_disabled true` |
| 182 | to turn this on (any other value leaves the greylist enabled). |
| 183 | |
Elliott Hughes | c45380d | 2016-10-05 14:35:00 -0700 | [diff] [blame] | 184 | ``` |
| 185 | $ readelf --dynamic libBroken.so | grep NEEDED |
| 186 | 0x00000001 (NEEDED) Shared library: [libnativehelper.so] |
| 187 | 0x00000001 (NEEDED) Shared library: [libutils.so] |
| 188 | 0x00000001 (NEEDED) Shared library: [libstagefright_foundation.so] |
| 189 | 0x00000001 (NEEDED) Shared library: [libmedia_jni.so] |
| 190 | 0x00000001 (NEEDED) Shared library: [liblog.so] |
| 191 | 0x00000001 (NEEDED) Shared library: [libdl.so] |
| 192 | 0x00000001 (NEEDED) Shared library: [libz.so] |
| 193 | 0x00000001 (NEEDED) Shared library: [libstdc++.so] |
| 194 | 0x00000001 (NEEDED) Shared library: [libm.so] |
| 195 | 0x00000001 (NEEDED) Shared library: [libc.so] |
| 196 | ``` |
| 197 | |
| 198 | *Potential problems*: starting from API 24 the dynamic linker will not |
| 199 | load private libraries, preventing the application from loading. |
| 200 | |
| 201 | *Resolution*: rewrite your native code to rely only on public API. As a |
| 202 | short term workaround, platform libraries without complex dependencies |
| 203 | (libcutils.so) can be copied to the project. As a long term solution |
| 204 | the relevant code must be copied to the project tree. SSL/Media/JNI |
| 205 | internal/binder APIs should not be accessed from the native code. When |
| 206 | necessary, native code should call appropriate public Java API methods. |
| 207 | |
| 208 | A complete list of public libraries is available within the NDK, under |
| 209 | platforms/android-API/usr/lib. |
| 210 | |
| 211 | Note: SSL/crypto is a special case, applications must NOT use platform |
| 212 | libcrypto and libssl libraries directly, even on older platforms. All |
| 213 | applications should use GMS Security Provider to ensure they are protected |
| 214 | from known vulnerabilities. |
| 215 | |
| 216 | |
| 217 | ## Missing Section Headers (Enforced for API level >= 24) |
| 218 | |
| 219 | Each ELF file has additional information contained in the section |
| 220 | headers. These headers must be present now, because the dynamic linker |
| 221 | uses them for sanity checking. Some developers strip them in an |
| 222 | attempt to obfuscate the binary and prevent reverse engineering. (This |
| 223 | doesn't really help because it is possible to reconstruct the stripped |
| 224 | information using widely-available tools.) |
| 225 | |
| 226 | ``` |
| 227 | $ readelf --header libBroken.so | grep 'section headers' |
| 228 | Start of section headers: 0 (bytes into file) |
| 229 | Size of section headers: 0 (bytes) |
| 230 | Number of section headers: 0 |
| 231 | ``` |
| 232 | |
| 233 | *Resolution*: remove the extra steps from your build that strip section |
| 234 | headers. |
| 235 | |
| 236 | ## Text Relocations (Enforced for API level >= 23) |
| 237 | |
| 238 | Starting with API 23, shared objects must not contain text |
| 239 | relocations. That is, the code must be loaded as is and must not be |
| 240 | modified. Such an approach reduces load time and improves security. |
| 241 | |
| 242 | The usual reason for text relocations is non-position independent |
| 243 | hand-written assembler. This is not common. Use the scanelf tool as |
| 244 | described in our documentation for further diagnostics: |
| 245 | |
| 246 | ``` |
| 247 | $ scanelf -qT libTextRel.so |
| 248 | libTextRel.so: (memory/data?) [0x15E0E2] in (optimized out: previous simd_broken_op1) [0x15E0E0] |
| 249 | libTextRel.so: (memory/data?) [0x15E3B2] in (optimized out: previous simd_broken_op2) [0x15E3B0] |
| 250 | ... |
| 251 | ``` |
| 252 | |
| 253 | If you have no scanelf tool available, it is possible to do a basic |
| 254 | check with readelf instead, look for either a TEXTREL entry or the |
| 255 | TEXTREL flag. Either alone is sufficient. (The value corresponding to the |
| 256 | TEXTREL entry is irrelevant and typically 0 --- simply the presence of |
| 257 | the TEXTREL entry declares that the .so contains text relocations). This |
| 258 | example has both indicators present: |
| 259 | |
| 260 | ``` |
| 261 | $ readelf --dynamic libTextRel.so | grep TEXTREL |
| 262 | 0x00000016 (TEXTREL) 0x0 |
| 263 | 0x0000001e (FLAGS) SYMBOLIC TEXTREL BIND_NOW |
| 264 | ``` |
| 265 | |
| 266 | Note: it is technically possible to have a shared object with the TEXTREL |
| 267 | entry/flag but without any actual text relocations. This doesn't happen |
| 268 | with the NDK, but if you're generating ELF files yourself make sure |
| 269 | you're not generating ELF files that claim to have text relocations, |
| 270 | because the Android dynamic linker trusts the entry/flag. |
| 271 | |
| 272 | *Potential problems*: Relocations enforce code pages being writable, and |
| 273 | wastefully increase the number of dirty pages in memory. The dynamic |
| 274 | linker has issued warnings about text relocations since Android K |
| 275 | (API 19), but on API 23 and above it refuses to load code with text |
| 276 | relocations. |
| 277 | |
| 278 | *Resolution*: rewrite assembler to be position independent to ensure |
| 279 | no text relocations are necessary. The |
| 280 | [Gentoo Textrels guide](https://wiki.gentoo.org/wiki/Hardened/Textrels_Guide) |
| 281 | has instructions for fixing text relocations, and more detailed |
| 282 | [scanelf documentation](https://wiki.gentoo.org/wiki/Hardened/PaX_Utilities). |
| 283 | |
| 284 | |
| 285 | ## Invalid DT_NEEDED Entries (Enforced for API level >= 23) |
| 286 | |
| 287 | While library dependencies (DT_NEEDED entries in the ELF headers) can be |
| 288 | absolute paths, that doesn't make sense on Android because you have |
| 289 | no control over where your library will be installed by the system. A |
| 290 | DT_NEEDED entry should be the same as the needed library's SONAME, |
| 291 | leaving the business of finding the library at runtime to the dynamic |
| 292 | linker. |
| 293 | |
| 294 | Before API 23, Android's dynamic linker ignored the full path, and |
| 295 | used only the basename (the part after the last ‘/') when looking |
| 296 | up the required libraries. Since API 23 the runtime linker will honor |
| 297 | the DT_NEEDED exactly and so it won't be able to load the library if |
| 298 | it is not present in that exact location on the device. |
| 299 | |
| 300 | Even worse, some build systems have bugs that cause them to insert |
| 301 | DT_NEEDED entries that point to a file on the build host, something that |
| 302 | cannot be found on the device. |
| 303 | |
| 304 | ``` |
| 305 | $ readelf --dynamic libSample.so | grep NEEDED |
| 306 | 0x00000001 (NEEDED) Shared library: [libm.so] |
| 307 | 0x00000001 (NEEDED) Shared library: [libc.so] |
| 308 | 0x00000001 (NEEDED) Shared library: [libdl.so] |
| 309 | 0x00000001 (NEEDED) Shared library: |
| 310 | [C:\Users\build\Android\ci\jni\libBroken.so] |
| 311 | ``` |
| 312 | |
| 313 | *Potential problems*: before API 23 the DT_NEEDED entry's basename was |
| 314 | used, but starting from API 23 the Android runtime will try to load the |
| 315 | library using the path specified, and that path won't exist on the |
| 316 | device. There are broken third-party toolchains/build systems that use |
| 317 | a path on a build host instead of the SONAME. |
| 318 | |
| 319 | *Resolution*: make sure all required libraries are referenced by SONAME |
| 320 | only. It is better to let the runtime linker to find and load those |
| 321 | libraries as the location may change from device to device. |
| 322 | |
| 323 | |
| 324 | ## Missing SONAME (Enforced for API level >= 23) |
| 325 | |
| 326 | Each ELF shared object (“native library”) must have a SONAME (Shared |
| 327 | Object Name) attribute. The NDK toolchain adds this attribute by default, |
| 328 | so its absence indicates either a misconfigured alternative toolchain |
| 329 | or a misconfiguration in your build system. A missing SONAME may lead |
| 330 | to runtime issues such as the wrong library being loaded: the filename |
| 331 | is used instead when this attribute is missing. |
| 332 | |
| 333 | ``` |
| 334 | $ readelf --dynamic libWithSoName.so | grep SONAME |
| 335 | 0x0000000e (SONAME) Library soname: [libWithSoName.so] |
| 336 | ``` |
| 337 | |
| 338 | *Potential problems*: namespace conflicts may lead to the wrong library |
| 339 | being loaded at runtime, which leads to crashes when required symbols |
| 340 | are not found, or you try to use an ABI-incompatible library that isn't |
| 341 | the library you were expecting. |
| 342 | |
| 343 | *Resolution*: the current NDK generates the correct SONAME by |
| 344 | default. Ensure you're using the current NDK and that you haven't |
| 345 | configured your build system to generate incorrect SONAME entries (using |
| 346 | the -soname linker option). |
Elliott Hughes | 77e8757 | 2016-10-07 15:59:58 -0700 | [diff] [blame] | 347 | |
Elliott Hughes | fb9ce28 | 2019-04-22 08:57:36 -0700 | [diff] [blame] | 348 | ## `__register_atfork` (Available in API level >= 23) |
| 349 | |
| 350 | To allow `atfork` and `pthread_atfork` handlers to be unregistered on |
| 351 | `dlclose`, the implementation changed in API level 23. Unfortunately this |
| 352 | requires a new libc function `__register_atfork`. Code using these functions |
| 353 | that is built with a target API level >= 23 therefore will not load on earlier |
| 354 | versions of Android, with an error referencing `__register_atfork`. |
| 355 | |
| 356 | *Resolution*: build your code with an NDK target API level that matches your |
| 357 | app's minimum API level, or avoid using `atfork`/`pthread_atfork`. |
Elliott Hughes | 77e8757 | 2016-10-07 15:59:58 -0700 | [diff] [blame] | 358 | |
Elliott Hughes | d6f91ce | 2017-04-17 16:01:23 -0700 | [diff] [blame] | 359 | ## DT_RUNPATH support (Available in API level >= 24) |
| 360 | |
| 361 | If an ELF file contains a DT_RUNPATH entry, the directories listed there |
| 362 | will be searched to resolve DT_NEEDED entries. The string `${ORIGIN}` will |
| 363 | be rewritten at runtime to the directory containing the ELF file. This |
| 364 | allows the use of relative paths. The `${LIB}` and `${PLATFORM}` |
| 365 | substitutions supported on some systems are not currently implemented on |
| 366 | Android. |
| 367 | |
| 368 | |
Elliott Hughes | 1777aaf | 2016-11-18 12:28:01 -0800 | [diff] [blame] | 369 | ## Writable and Executable Segments (Enforced for API level >= 26) |
Elliott Hughes | 77e8757 | 2016-10-07 15:59:58 -0700 | [diff] [blame] | 370 | |
| 371 | Each segment in an ELF file has associated flags that tell the |
| 372 | dynamic linker what permissions to give the corresponding page in |
| 373 | memory. For security, data shouldn't be executable and code shouldn't be |
| 374 | writable. This means that the W (for Writable) and E (for Executable) |
| 375 | flags should be mutually exclusive. This wasn't historically enforced, |
| 376 | but is now. |
| 377 | |
| 378 | ``` |
| 379 | $ readelf --program-headers -W libBadFlags.so | grep WE |
| 380 | LOAD 0x000000 0x00000000 0x00000000 0x4c01d 0x4c01d RWE 0x1000 |
| 381 | ``` |
| 382 | |
Elliott Hughes | 4cc5a60 | 2016-11-15 16:54:16 -0800 | [diff] [blame] | 383 | *Resolution*: we're aware of one middleware product that introduces these |
| 384 | into your app. The middleware vendor is aware of the problem and has a fix |
| 385 | available. |
Dimitry Ivanov | 12b9187 | 2016-11-16 12:29:37 -0800 | [diff] [blame] | 386 | |
Elliott Hughes | 1777aaf | 2016-11-18 12:28:01 -0800 | [diff] [blame] | 387 | ## Invalid ELF header/section headers (Enforced for API level >= 26) |
Dimitry Ivanov | 12b9187 | 2016-11-16 12:29:37 -0800 | [diff] [blame] | 388 | |
Elliott Hughes | 1777aaf | 2016-11-18 12:28:01 -0800 | [diff] [blame] | 389 | In API level 26 and above the dynamic linker checks more values in |
| 390 | the ELF header and section headers and fails if they are invalid. |
Dimitry Ivanov | 12b9187 | 2016-11-16 12:29:37 -0800 | [diff] [blame] | 391 | |
| 392 | *Example error* |
Elliott Hughes | 1777aaf | 2016-11-18 12:28:01 -0800 | [diff] [blame] | 393 | ``` |
Dimitry Ivanov | 12b9187 | 2016-11-16 12:29:37 -0800 | [diff] [blame] | 394 | dlopen failed: "/data/data/com.example.bad/lib.so" has unsupported e_shentsize: 0x0 (expected 0x28) |
Elliott Hughes | 1777aaf | 2016-11-18 12:28:01 -0800 | [diff] [blame] | 395 | ``` |
Dimitry Ivanov | 12b9187 | 2016-11-16 12:29:37 -0800 | [diff] [blame] | 396 | |
Elliott Hughes | 1777aaf | 2016-11-18 12:28:01 -0800 | [diff] [blame] | 397 | *Resolution*: don't use tools that produce invalid/malformed |
| 398 | ELF files. Note that using them puts application under high risk of |
| 399 | being incompatible with future versions of Android. |
Dimitry Ivanov | 5d1753f | 2016-12-14 14:42:44 -0800 | [diff] [blame] | 400 | |
| 401 | ## Enable logging of dlopen/dlsym and library loading errors for apps (Available in Android O) |
| 402 | |
Elliott Hughes | c5f7824 | 2017-12-05 09:18:01 -0800 | [diff] [blame] | 403 | Starting with Android O it is possible to enable logging of dynamic |
| 404 | linker activity for debuggable apps by setting a property corresponding |
| 405 | to the fully-qualified name of the specific app: |
Dimitry Ivanov | 5d1753f | 2016-12-14 14:42:44 -0800 | [diff] [blame] | 406 | ``` |
Elliott Hughes | c5f7824 | 2017-12-05 09:18:01 -0800 | [diff] [blame] | 407 | adb shell setprop debug.ld.app.com.example.myapp dlerror,dlopen,dlsym |
Dimitry Ivanov | 5d1753f | 2016-12-14 14:42:44 -0800 | [diff] [blame] | 408 | adb logcat |
| 409 | ``` |
| 410 | |
Elliott Hughes | c5f7824 | 2017-12-05 09:18:01 -0800 | [diff] [blame] | 411 | Any combination of `dlerror`, `dlopen`, and `dlsym` can be used. There's |
| 412 | no separate `dlclose` option: `dlopen` covers both loading and unloading |
| 413 | of libraries. Note also that `dlerror` doesn't correspond to actual |
| 414 | calls of dlerror(3) but to any time the dynamic linker writes to its |
| 415 | internal error buffer, so you'll see any errors the dynamic linker would |
| 416 | have reported, even if the code you're debugging doesn't actually call |
| 417 | dlerror(3) itself. |
Dimitry Ivanov | 5d1753f | 2016-12-14 14:42:44 -0800 | [diff] [blame] | 418 | |
Elliott Hughes | c5f7824 | 2017-12-05 09:18:01 -0800 | [diff] [blame] | 419 | On userdebug and eng builds it is possible to enable tracing for the |
| 420 | whole system by using the `debug.ld.all` system property instead of |
| 421 | app-specific one. For example, to enable logging of all dlopen(3) |
| 422 | (and thus dclose(3)) calls, and all failures, but not dlsym(3) calls: |
Dimitry Ivanov | 5d1753f | 2016-12-14 14:42:44 -0800 | [diff] [blame] | 423 | ``` |
| 424 | adb shell setprop debug.ld.all dlerror,dlopen |
| 425 | ``` |
| 426 | |
Elliott Hughes | bcbce9b | 2017-10-25 15:02:36 -0700 | [diff] [blame] | 427 | ## dlclose interacts badly with thread local variables with non-trivial destructors |
| 428 | |
| 429 | Android allows `dlclose` to unload a library even if there are still |
| 430 | thread-local variables with non-trivial destructors. This leads to |
| 431 | crashes when a thread exits and attempts to call the destructor, the |
Elliott Hughes | e61e0cd | 2018-01-19 10:33:41 -0800 | [diff] [blame] | 432 | code for which has been unloaded (as in [issue 360], fixed in P). |
Elliott Hughes | bcbce9b | 2017-10-25 15:02:36 -0700 | [diff] [blame] | 433 | |
| 434 | [issue 360]: https://github.com/android-ndk/ndk/issues/360 |
| 435 | |
| 436 | Not calling `dlclose` or ensuring that your library has `RTLD_NODELETE` |
| 437 | set (so that calls to `dlclose` don't actually unload the library) |
| 438 | are possible workarounds. |
| 439 | |
Elliott Hughes | e61e0cd | 2018-01-19 10:33:41 -0800 | [diff] [blame] | 440 | | | Pre-M | M+ | P+ | |
| 441 | | ----------------- | -------------------------- | ------- | ----- | |
| 442 | | No workaround | Works for static STL | Broken | Works | |
| 443 | | `-Wl,-z,nodelete` | Works for static STL | Works | Works | |
| 444 | | No `dlclose` | Works | Works | Works | |
Elliott Hughes | 3770d93 | 2019-03-26 08:52:07 -0700 | [diff] [blame] | 445 | |
| 446 | ## Use of IFUNC in libc (True for all API levels on devices running Q) |
| 447 | |
| 448 | Starting with Android Q (API level 29), libc uses |
| 449 | [IFUNC](https://sourceware.org/glibc/wiki/GNU_IFUNC) functionality in |
| 450 | the dynamic linker to choose optimized assembler routines at run time |
| 451 | rather than at build time. This lets us use the same `libc.so` on all |
| 452 | devices, and is similar to what other OSes already did. Because the zygote |
| 453 | uses the C library, this decision is made long before we know what API |
| 454 | level an app targets, so all code sees the new IFUNC-using C library. |
| 455 | Most apps should be unaffected by this change, but apps that hook or try to |
| 456 | detect hooking of C library functions might need to fix their code to cope |
| 457 | with IFUNC relocations. The affected functions are from `<string.h>`, but |
| 458 | may expand to include more functions (and more libraries) in future. |
Elliott Hughes | 6663f55 | 2020-01-24 14:36:10 -0800 | [diff] [blame] | 459 | |
| 460 | ## Relative relocations (RELR) |
| 461 | |
| 462 | Android added experimental support for RELR relative relocations |
| 463 | in API level 28, but using `SHT_` and `DT_` constants in the space |
| 464 | reserved for OS private use. |
| 465 | |
| 466 | API level 30 added support for ELF files using the official `SHT_` and |
| 467 | `DT_` constants. |
| 468 | |
| 469 | The RELR encoding is unrelated to the earlier "packed relocations" |
| 470 | format available from API level 23. |
| 471 | |
| 472 | There are no plans to remove support for ELF files using the older |
| 473 | OS private use constants for RELR, nor for ELF files using packed |
| 474 | relocations. |