blob: e094a1d53cd43040dae2d579f847cd8ed0eb5520 [file] [log] [blame] [view]
Elliott Hughes8f3f1912019-08-15 08:19:49 -07001# Android linker changes for NDK developers
Elliott Hughesc45380d2016-10-05 14:35:00 -07002
3This document details important changes related to native code
4loading in various Android releases.
5
Elliott Hughes8f3f1912019-08-15 08:19:49 -07006See also [bionic status](docs/status.md) for general libc/libm/libdl
7behavior changes.
8
Elliott Hughes04277602020-10-30 16:36:16 -07009See also the
Elliott Hughes9c06d162023-10-04 23:36:14 +000010[unwinder documentation](https://android.googlesource.com/platform/system/unwinding/+/refs/heads/main/libunwindstack/AndroidVersions.md)
Elliott Hughes04277602020-10-30 16:36:16 -070011for details about changes in stack unwinding (crash dumps) between
12different releases.
13
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +000014Required tools: the NDK has an `llvm-readelf` binary that understands all the
15architecture-specific details of all Android's supported architectures. Recent
16versions of Android also have toybox readelf on the device.
Elliott Hughesc45380d2016-10-05 14:35:00 -070017
18
Elliott Hughes9e3d5272017-01-13 11:07:00 -080019## How we manage incompatible changes
20
21Our general practice with dynamic linker behavior changes is that they
22will be tied to an app's target API level:
23
24* Below the affected API level we'll preserve the old behavior or issue
25a warning, as appropriate.
26
27* At the affected API level and above, we’ll refuse to load the library.
28
29* Warnings about any behavior change that will affect a library if you
30increase your target API level will appear in logcat when that library
31is loaded, even if you're not yet targeting that API level.
32
33* On a developer preview build, dynamic linker warnings will also show up
34as toasts. Experience has shown that many developers don’t habitually
35check logcat for warnings until their app stops functioning, so the
36toasts help bring some visibility to the issues before it's too late.
37
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +000038
Elliott Hughesdc660732018-05-01 11:27:46 -070039## Changes to library dependency resolution
40
41Until it was [fixed](https://issuetracker.google.com/36950617) in
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +000042API level 18, Android didn't include the application library directory
Elliott Hughesdc660732018-05-01 11:27:46 -070043on the dynamic linker's search path. This meant that apps
44had to call `dlopen` or `System.loadLibrary` on all transitive
45dependencies before loading their main library. Worse, until it was
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +000046[fixed](https://issuetracker.google.com/36935779) in API level 18, the
Elliott Hughesdc660732018-05-01 11:27:46 -070047dynamic linker's caching code cached failures too, so it was necessary
48to topologically sort your libraries and load them in reverse order.
49
Elliott Hughes89672112024-12-18 07:08:44 -080050This issue is no longer relevant to most developers,
51but if you need to support Android devices running OS versions older than
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +000052API level 23, you might want to consider
Elliott Hughes89672112024-12-18 07:08:44 -080053[ReLinker](https://github.com/KeepSafe/ReLinker) or
54[SoLoader](https://github.com/facebook/SoLoader),
55which claim to solve these problems automatically.
Elliott Hughes9e3d5272017-01-13 11:07:00 -080056
Elliott Hughes3230b682019-08-05 16:40:52 -070057Alternatively, if you don't have too many dependencies, it can be easiest to
58simply link all of your code into one big library and sidestep the details of
59library and symbol lookup changes on all past (and future) Android versions.
60
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +000061
Elliott Hughes1777aaf2016-11-18 12:28:01 -080062## Changes to library search order
63
64We have made various fixes to library search order when resolving symbols.
65
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +000066With API level 22, load order switched from depth-first to breadth-first to
Elliott Hughes1777aaf2016-11-18 12:28:01 -080067fix dlsym(3).
68
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +000069Before API level 23, the default search order was to try the main executable,
Elliott Hughes1777aaf2016-11-18 12:28:01 -080070LD_PRELOAD libraries, the library itself, and its DT_NEEDED libraries
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +000071in that order. For API level 23 and later, for any given library, the dynamic
Elliott Hughes1777aaf2016-11-18 12:28:01 -080072linker divides other libraries into the global group and the local
73group. The global group is shared by all libraries and contains the main
74executable, LD_PRELOAD libraries, and any library with the DF_1_GLOBAL
75flag set (by passing “-z global” to ld(1)). The local group is
76the breadth-first transitive closure of the library and its DT_NEEDED
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +000077libraries. The API level 23 dynamic linker searches the global group followed by
Elliott Hughes1777aaf2016-11-18 12:28:01 -080078the local group. This allows ASAN, for example, to ensure that it can
79intercept any symbol.
80
Elliott Hughes89672112024-12-18 07:08:44 -080081This issue is no longer relevant to most developers,
82but if you need to support Android devices running OS versions older than
83API level 23, you might want to consider
84[ReLinker](https://github.com/KeepSafe/ReLinker) or
85[SoLoader](https://github.com/facebook/SoLoader),
86which claim to solve these problems automatically.
87
88Alternatively, if you don't have too many dependencies, it can be easiest to
89simply link all of your code into one big library and sidestep the details of
90library and symbol lookup changes on all past (and future) Android versions.
91
Elliott Hughes1777aaf2016-11-18 12:28:01 -080092
Elliott Hughes8e4b6b62019-06-05 08:28:55 -070093## LD_PRELOAD and 32/64 bit
94
95LD_PRELOAD applies to both 32- and 64-bit processes. This means that you
96should avoid saying something like `/system/lib/libfoo.so` and just say
97`libfoo.so` instead, letting the dynamic linker find the correct library
98on its search path.
99
100
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800101## RTLD_LOCAL (Available in API level >= 23)
102
103The dlopen(3) RTLD_LOCAL flag used to be ignored but is implemented
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000104correctly in API level 23 and later. Note that RTLD_LOCAL is the default,
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800105so even calls to dlopen(3) that didn’t explicitly use RTLD_LOCAL will
106be affected (unless they explicitly used RTLD_GLOBAL). With RTLD_LOCAL,
107symbols will not be made available to libraries loaded by later calls
108to dlopen(3) (as opposed to being referenced by DT_NEEDED entries).
109
110
Elliott Hughesfae58f52025-03-25 08:03:49 -0700111## GNU hashes (Available in API level >= 23)
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800112
Elliott Hughes91219ec2024-03-19 00:51:08 +0000113The GNU hash style available with `--hash-style=gnu` allows faster
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000114symbol lookup and is supported by Android's dynamic linker in API level 23 and
Elliott Hughes91219ec2024-03-19 00:51:08 +0000115above. Use `--hash-style=both` if you want to build code that uses this
116feature in new enough releases but still works on older releases.
117If you're using the NDK, clang chooses the right option
118(automatically)[https://github.com/android/ndk/issues/2005].
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800119
120
121## Correct soname/path handling (Available in API level >= 23)
122
123The dynamic linker now understands the difference
Elliott Hughes8aa1deb2023-04-13 14:45:00 +0000124between a library’s soname and its path (public bug
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800125https://code.google.com/p/android/issues/detail?id=6670). API level 23
126is the first release where search by soname is implemented. Earlier
127releases would assume that the basename of the library was the soname,
128and used that to search for already-loaded libraries. For example,
129`dlopen("/this/directory/does/not/exist/libc.so", RTLD_NOW)` would
130find `/system/lib/libc.so` because it’s already loaded. This also meant
131that it was impossible to have two libraries `"dir1/libx.so"` and
132`"dir2/libx.so"` --- the dynamic linker couldn’t tell the difference
133and would always use whichever was loaded first, even if you explicitly
134tried to load both. This also applied to DT_NEEDED entries.
135
136Some apps have bad DT_NEEDED entries (usually absolute paths on the build
137machine’s file system) that used to work because we ignored everything
138but the basename. These apps will fail to load on API level 23 and above.
139
140
141## Symbol versioning (Available in API level >= 23)
142
143Symbol versioning allows libraries to provide better backwards
144compatibility. For example, if a library author knowingly changes
145the behavior of a function, they can provide two versions in the same
146library so that old code gets the old version and new code gets the new
147version. This is supported in API level 23 and above.
148
149
150## Opening shared libraries directly from an APK
151
152In API level 23 and above, it’s possible to open a .so file directly from
153your APK. Just use `System.loadLibrary("foo")` exactly as normal but set
154`android:extractNativeLibs="false"` in your `AndroidManifest.xml`. In
155older releases, the .so files were extracted from the APK file
156at install time. This meant that they took up space in your APK and
157again in your installation directory (and this was counted against you
158and reported to the user as space taken up by your app). Any .so file
159that you want to load directly from your APK must be page aligned
160(on a 4096-byte boundary) in the zip file and stored uncompressed.
161Current versions of the zipalign tool take care of alignment.
162
Elliott Hughes9b81e582023-08-10 19:26:36 +0000163Note that in API level 23 and above dlopen(3) can open a library from
164any zip file, not just an APK. Just give dlopen(3) a path of the form
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800165"my_zip_file.zip!/libs/libstuff.so". As with APKs, the library must be
166page-aligned and stored uncompressed for this to work.
167
168
Elliott Hughesc45380d2016-10-05 14:35:00 -0700169## Private API (Enforced for API level >= 24)
170
171Native libraries must use only public API, and must not link against
Elliott Hughesb90421a2024-03-29 19:24:21 +0000172non-NDK platform libraries. On devices running API level 24 or later,
173this rule is enforced and applications are no longer able to load all
174non-NDK platform libraries. This was to prevent future issues similar
175to the disruption caused when Android switched from OpenSSL to BoringSSL
176at API level 23.
177
178The rule is enforced by the dynamic linker, so non-public libraries
Elliott Hughesc45380d2016-10-05 14:35:00 -0700179are not accessible regardless of the way code tries to load them:
Elliott Hughesb90421a2024-03-29 19:24:21 +0000180System.loadLibrary(), DT_NEEDED entries, and direct calls to dlopen(3)
Elliott Hughesc45380d2016-10-05 14:35:00 -0700181will all work exactly the same.
182
Elliott Hughesb90421a2024-03-29 19:24:21 +0000183In order to reduce the user impact of this transition, we identified
184a set of libraries that saw significant use from Google Play's
185most-installed apps and were feasible for us to support in the
Elliott Hughesc45380d2016-10-05 14:35:00 -0700186short term (including libandroid_runtime.so, libcutils.so, libcrypto.so,
Elliott Hughesb90421a2024-03-29 19:24:21 +0000187and libssl.so). In order to give app developers more time to transition,
188we allowed access to these libraries for apps with a target API level < 24.
189On devices running API level 26 to API level 30, this compatibility mode could be
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000190disabled by setting a system property (`debug.ld.greylist_disabled`).
Elliott Hughesb90421a2024-03-29 19:24:21 +0000191This property is ignored on devices running API level 31 and later.
Elliott Hughes9e27e582017-03-23 17:42:49 -0700192
Elliott Hughesc45380d2016-10-05 14:35:00 -0700193```
194$ readelf --dynamic libBroken.so | grep NEEDED
195 0x00000001 (NEEDED) Shared library: [libnativehelper.so]
196 0x00000001 (NEEDED) Shared library: [libutils.so]
197 0x00000001 (NEEDED) Shared library: [libstagefright_foundation.so]
198 0x00000001 (NEEDED) Shared library: [libmedia_jni.so]
199 0x00000001 (NEEDED) Shared library: [liblog.so]
200 0x00000001 (NEEDED) Shared library: [libdl.so]
201 0x00000001 (NEEDED) Shared library: [libz.so]
202 0x00000001 (NEEDED) Shared library: [libstdc++.so]
203 0x00000001 (NEEDED) Shared library: [libm.so]
204 0x00000001 (NEEDED) Shared library: [libc.so]
205```
206
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000207*Potential problems*: starting from API level 24 the dynamic linker will not
Elliott Hughesc45380d2016-10-05 14:35:00 -0700208load private libraries, preventing the application from loading.
209
210*Resolution*: rewrite your native code to rely only on public API. As a
211short term workaround, platform libraries without complex dependencies
212(libcutils.so) can be copied to the project. As a long term solution
213the relevant code must be copied to the project tree. SSL/Media/JNI
214internal/binder APIs should not be accessed from the native code. When
215necessary, native code should call appropriate public Java API methods.
216
217A complete list of public libraries is available within the NDK, under
218platforms/android-API/usr/lib.
219
220Note: SSL/crypto is a special case, applications must NOT use platform
221libcrypto and libssl libraries directly, even on older platforms. All
222applications should use GMS Security Provider to ensure they are protected
223from known vulnerabilities.
224
225
226## Missing Section Headers (Enforced for API level >= 24)
227
228Each ELF file has additional information contained in the section
229headers. These headers must be present now, because the dynamic linker
Elliott Hughes68ae6ad2020-07-21 16:11:30 -0700230uses them for validity checking. Some developers strip them in an
Elliott Hughesc45380d2016-10-05 14:35:00 -0700231attempt to obfuscate the binary and prevent reverse engineering. (This
232doesn't really help because it is possible to reconstruct the stripped
233information using widely-available tools.)
234
235```
Elliott Hughes8e4e6f92024-02-13 15:25:13 +0000236$ readelf --headers libBroken.so | grep 'section headers'
Elliott Hughesc45380d2016-10-05 14:35:00 -0700237 Start of section headers: 0 (bytes into file)
238 Size of section headers: 0 (bytes)
239 Number of section headers: 0
240```
241
242*Resolution*: remove the extra steps from your build that strip section
243headers.
244
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000245
Elliott Hughesc45380d2016-10-05 14:35:00 -0700246## Text Relocations (Enforced for API level >= 23)
247
Elliott Hughesb90421a2024-03-29 19:24:21 +0000248Apps with a target API level >= 23 cannot load shared objects that contain text
249relocations. Such an approach reduces load time and improves security. This was
250only a change for 32-bit, because 64-bit never supported text relocations.
Elliott Hughesc45380d2016-10-05 14:35:00 -0700251
Elliott Hughesb90421a2024-03-29 19:24:21 +0000252The usual reason for text relocations was non-position independent
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000253hand-written assembler. This is not common. You can use the scanelf tool
254from the pax-utils debian package for further diagnostics:
Elliott Hughesc45380d2016-10-05 14:35:00 -0700255
256```
257$ scanelf -qT libTextRel.so
258 libTextRel.so: (memory/data?) [0x15E0E2] in (optimized out: previous simd_broken_op1) [0x15E0E0]
259 libTextRel.so: (memory/data?) [0x15E3B2] in (optimized out: previous simd_broken_op2) [0x15E3B0]
260 ...
261```
262
263If you have no scanelf tool available, it is possible to do a basic
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000264check with readelf instead. Look for either a TEXTREL entry or the
Elliott Hughesc45380d2016-10-05 14:35:00 -0700265TEXTREL flag. Either alone is sufficient. (The value corresponding to the
266TEXTREL entry is irrelevant and typically 0 --- simply the presence of
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000267the TEXTREL entry declares that the .so contains text relocations.) This
Elliott Hughesc45380d2016-10-05 14:35:00 -0700268example has both indicators present:
269
270```
271$ readelf --dynamic libTextRel.so | grep TEXTREL
272 0x00000016 (TEXTREL) 0x0
273 0x0000001e (FLAGS) SYMBOLIC TEXTREL BIND_NOW
274```
275
276Note: it is technically possible to have a shared object with the TEXTREL
277entry/flag but without any actual text relocations. This doesn't happen
278with the NDK, but if you're generating ELF files yourself make sure
279you're not generating ELF files that claim to have text relocations,
280because the Android dynamic linker trusts the entry/flag.
281
282*Potential problems*: Relocations enforce code pages being writable, and
283wastefully increase the number of dirty pages in memory. The dynamic
Elliott Hughesb90421a2024-03-29 19:24:21 +0000284linker issued warnings about text relocations from API level 19, but on API
285level 23 and above refuses to load code with text relocations.
Elliott Hughesc45380d2016-10-05 14:35:00 -0700286
287*Resolution*: rewrite assembler to be position independent to ensure
288no text relocations are necessary. The
289[Gentoo Textrels guide](https://wiki.gentoo.org/wiki/Hardened/Textrels_Guide)
290has instructions for fixing text relocations, and more detailed
291[scanelf documentation](https://wiki.gentoo.org/wiki/Hardened/PaX_Utilities).
292
293
294## Invalid DT_NEEDED Entries (Enforced for API level >= 23)
295
296While library dependencies (DT_NEEDED entries in the ELF headers) can be
297absolute paths, that doesn't make sense on Android because you have
298no control over where your library will be installed by the system. A
299DT_NEEDED entry should be the same as the needed library's SONAME,
300leaving the business of finding the library at runtime to the dynamic
301linker.
302
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000303Before API level 23, Android's dynamic linker ignored the full path, and
Elliott Hughesc45380d2016-10-05 14:35:00 -0700304used only the basename (the part after the last ‘/') when looking
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000305up the required libraries. Since API level 23 the runtime linker will honor
Elliott Hughesc45380d2016-10-05 14:35:00 -0700306the DT_NEEDED exactly and so it won't be able to load the library if
307it is not present in that exact location on the device.
308
309Even worse, some build systems have bugs that cause them to insert
310DT_NEEDED entries that point to a file on the build host, something that
311cannot be found on the device.
312
313```
314$ readelf --dynamic libSample.so | grep NEEDED
315 0x00000001 (NEEDED) Shared library: [libm.so]
316 0x00000001 (NEEDED) Shared library: [libc.so]
317 0x00000001 (NEEDED) Shared library: [libdl.so]
318 0x00000001 (NEEDED) Shared library:
319[C:\Users\build\Android\ci\jni\libBroken.so]
320```
321
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000322*Potential problems*: before API level 23 the DT_NEEDED entry's basename was
323used, but starting from API level 23 the Android runtime will try to load the
Elliott Hughesc45380d2016-10-05 14:35:00 -0700324library using the path specified, and that path won't exist on the
325device. There are broken third-party toolchains/build systems that use
326a path on a build host instead of the SONAME.
327
328*Resolution*: make sure all required libraries are referenced by SONAME
329only. It is better to let the runtime linker to find and load those
330libraries as the location may change from device to device.
331
332
333## Missing SONAME (Enforced for API level >= 23)
334
Elliott Hughes8aa1deb2023-04-13 14:45:00 +0000335Each ELF shared object (“native library”) must have a SONAME
336(Shared Object Name) attribute. The NDK build systems add this
337attribute by default, so its absence (or an incorrect soname) indicates
338a misconfiguration in your build system. A missing SONAME may lead to
339runtime issues such as the wrong library being loaded: the filename is
340used instead when this attribute is missing.
Elliott Hughesc45380d2016-10-05 14:35:00 -0700341
342```
343$ readelf --dynamic libWithSoName.so | grep SONAME
344 0x0000000e (SONAME) Library soname: [libWithSoName.so]
345```
346
347*Potential problems*: namespace conflicts may lead to the wrong library
348being loaded at runtime, which leads to crashes when required symbols
349are not found, or you try to use an ABI-incompatible library that isn't
350the library you were expecting.
351
352*Resolution*: the current NDK generates the correct SONAME by
353default. Ensure you're using the current NDK and that you haven't
354configured your build system to generate incorrect SONAME entries (using
Elliott Hughes8aa1deb2023-04-13 14:45:00 +0000355the `-soname` linker option).
Elliott Hughes77e87572016-10-07 15:59:58 -0700356
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000357
Elliott Hughesfb9ce282019-04-22 08:57:36 -0700358## `__register_atfork` (Available in API level >= 23)
359
360To allow `atfork` and `pthread_atfork` handlers to be unregistered on
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000361`dlclose`, API level 23 added a new libc function `__register_atfork`.
362This means that code using `atfork` or `pthread_atfork` functions that is
Elliott Hughesb90421a2024-03-29 19:24:21 +0000363built with a `minSdkVersion` >= 23 will not load on earlier versions of
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000364Android, with an error referencing `__register_atfork`.
Elliott Hughesfb9ce282019-04-22 08:57:36 -0700365
Elliott Hughesb90421a2024-03-29 19:24:21 +0000366*Resolution*: build your code with `minSdkVersion` that matches the minimum
367API level you actually support, or avoid using `atfork`/`pthread_atfork`.
Elliott Hughes77e87572016-10-07 15:59:58 -0700368
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000369
Elliott Hughesd6f91ce2017-04-17 16:01:23 -0700370## DT_RUNPATH support (Available in API level >= 24)
371
372If an ELF file contains a DT_RUNPATH entry, the directories listed there
373will be searched to resolve DT_NEEDED entries. The string `${ORIGIN}` will
374be rewritten at runtime to the directory containing the ELF file. This
375allows the use of relative paths. The `${LIB}` and `${PLATFORM}`
376substitutions supported on some systems are not currently implemented on
377Android.
378
379
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800380## Writable and Executable Segments (Enforced for API level >= 26)
Elliott Hughes77e87572016-10-07 15:59:58 -0700381
382Each segment in an ELF file has associated flags that tell the
383dynamic linker what permissions to give the corresponding page in
384memory. For security, data shouldn't be executable and code shouldn't be
385writable. This means that the W (for Writable) and E (for Executable)
386flags should be mutually exclusive. This wasn't historically enforced,
387but is now.
388
389```
390$ readelf --program-headers -W libBadFlags.so | grep WE
391 LOAD 0x000000 0x00000000 0x00000000 0x4c01d 0x4c01d RWE 0x1000
392```
393
Elliott Hughes4cc5a602016-11-15 16:54:16 -0800394*Resolution*: we're aware of one middleware product that introduces these
395into your app. The middleware vendor is aware of the problem and has a fix
396available.
Dimitry Ivanov12b91872016-11-16 12:29:37 -0800397
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000398
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800399## Invalid ELF header/section headers (Enforced for API level >= 26)
Dimitry Ivanov12b91872016-11-16 12:29:37 -0800400
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800401In API level 26 and above the dynamic linker checks more values in
402the ELF header and section headers and fails if they are invalid.
Dimitry Ivanov12b91872016-11-16 12:29:37 -0800403
404*Example error*
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800405```
Dimitry Ivanov12b91872016-11-16 12:29:37 -0800406dlopen failed: "/data/data/com.example.bad/lib.so" has unsupported e_shentsize: 0x0 (expected 0x28)
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800407```
Dimitry Ivanov12b91872016-11-16 12:29:37 -0800408
Elliott Hughes1777aaf2016-11-18 12:28:01 -0800409*Resolution*: don't use tools that produce invalid/malformed
410ELF files. Note that using them puts application under high risk of
411being incompatible with future versions of Android.
Dimitry Ivanov5d1753f2016-12-14 14:42:44 -0800412
Dimitry Ivanov5d1753f2016-12-14 14:42:44 -0800413
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000414## Enable logging of dlopen/dlsym and library loading errors for apps (Available for API level >= 26)
415
Elliott Hughesb90421a2024-03-29 19:24:21 +0000416On devices running API level 26 or later you can enable logging of dynamic
Elliott Hughesc5f78242017-12-05 09:18:01 -0800417linker activity for debuggable apps by setting a property corresponding
418to the fully-qualified name of the specific app:
Dimitry Ivanov5d1753f2016-12-14 14:42:44 -0800419```
Elliott Hughesc5f78242017-12-05 09:18:01 -0800420adb shell setprop debug.ld.app.com.example.myapp dlerror,dlopen,dlsym
Dimitry Ivanov5d1753f2016-12-14 14:42:44 -0800421adb logcat
422```
423
Elliott Hughesc5f78242017-12-05 09:18:01 -0800424Any combination of `dlerror`, `dlopen`, and `dlsym` can be used. There's
425no separate `dlclose` option: `dlopen` covers both loading and unloading
426of libraries. Note also that `dlerror` doesn't correspond to actual
427calls of dlerror(3) but to any time the dynamic linker writes to its
428internal error buffer, so you'll see any errors the dynamic linker would
429have reported, even if the code you're debugging doesn't actually call
430dlerror(3) itself.
Dimitry Ivanov5d1753f2016-12-14 14:42:44 -0800431
Elliott Hughesc5f78242017-12-05 09:18:01 -0800432On userdebug and eng builds it is possible to enable tracing for the
433whole system by using the `debug.ld.all` system property instead of
434app-specific one. For example, to enable logging of all dlopen(3)
435(and thus dclose(3)) calls, and all failures, but not dlsym(3) calls:
Dimitry Ivanov5d1753f2016-12-14 14:42:44 -0800436```
437adb shell setprop debug.ld.all dlerror,dlopen
438```
439
Elliott Hughesfae58f52025-03-25 08:03:49 -0700440See also `LD_DEBUG`.
441
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000442
Elliott Hughesbcbce9b2017-10-25 15:02:36 -0700443## dlclose interacts badly with thread local variables with non-trivial destructors
444
445Android allows `dlclose` to unload a library even if there are still
446thread-local variables with non-trivial destructors. This leads to
447crashes when a thread exits and attempts to call the destructor, the
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000448code for which has been unloaded (as in [issue 360], fixed in API level 28).
Elliott Hughesbcbce9b2017-10-25 15:02:36 -0700449
450[issue 360]: https://github.com/android-ndk/ndk/issues/360
451
452Not calling `dlclose` or ensuring that your library has `RTLD_NODELETE`
453set (so that calls to `dlclose` don't actually unload the library)
454are possible workarounds.
455
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000456| | API level < 23 | >= 23 | >= 28 |
Elliott Hughese61e0cd2018-01-19 10:33:41 -0800457| ----------------- | -------------------------- | ------- | ----- |
458| No workaround | Works for static STL | Broken | Works |
459| `-Wl,-z,nodelete` | Works for static STL | Works | Works |
460| No `dlclose` | Works | Works | Works |
Elliott Hughes3770d932019-03-26 08:52:07 -0700461
Elliott Hughes3770d932019-03-26 08:52:07 -0700462
Elliott Hughes1ce910e2024-06-21 20:42:09 +0000463## ELF TLS (Available for API level >= 29)
464
465Android supports [ELF TLS](docs/elf-tls.md) starting at API level 29. Since
466NDK r26, clang will automatically enable ELF TLS for `minSdkVersion 29` or
467higher. Otherwise, the existing emutls implementation (which uses
468`pthread_key_create()` behind the scenes) will continue to be used. This
469means that convenient C/C++ thread-local syntax is available at any API level;
470at worst it will perform similarly to "roll your own" thread locals using
471`pthread_key_create()` but at best you'll get the performance benefit of
472ELF TLS, and the NDK will take care of the details.
473
474
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000475## Use of IFUNC in libc (True for all API levels on devices running Android 10)
476
Elliott Hughesb90421a2024-03-29 19:24:21 +0000477On devices running API level 29, libc uses
478[IFUNC](https://sourceware.org/glibc/wiki/GNU_IFUNC)
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000479functionality in the dynamic linker to choose optimized assembler routines at
480run time rather than at build time. This lets us use the same `libc.so` on all
Elliott Hughes3770d932019-03-26 08:52:07 -0700481devices, and is similar to what other OSes already did. Because the zygote
482uses the C library, this decision is made long before we know what API
483level an app targets, so all code sees the new IFUNC-using C library.
484Most apps should be unaffected by this change, but apps that hook or try to
485detect hooking of C library functions might need to fix their code to cope
486with IFUNC relocations. The affected functions are from `<string.h>`, but
487may expand to include more functions (and more libraries) in future.
Elliott Hughes6663f552020-01-24 14:36:10 -0800488
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000489
Elliott Hughes6663f552020-01-24 14:36:10 -0800490## Relative relocations (RELR)
491
492Android added experimental support for RELR relative relocations
493in API level 28, but using `SHT_` and `DT_` constants in the space
494reserved for OS private use.
495
496API level 30 added support for ELF files using the official `SHT_` and
497`DT_` constants.
498
499The RELR encoding is unrelated to the earlier "packed relocations"
500format available from API level 23.
501
502There are no plans to remove support for ELF files using the older
503OS private use constants for RELR, nor for ELF files using packed
504relocations.
Elliott Hughesbb1cb032023-01-18 23:26:58 +0000505
Peter Collingbournee0ebca82024-03-26 15:39:42 -0700506Prior to API level 35, there was a bug that caused RELR relocations to
507be applied after packed relocations. This meant that ifunc resolvers
508referenced by `R_*_IRELATIVE` relocations in the packed relocation
509section would have been able to read globals with RELR relocations
510before they were relocated. The version of `lld` in the NDK has never
511produced binaries affected by this bug, but third-party toolchains
512should make sure not to store `R_*_IRELATIVE` relocations in packed
513relocation sections in order to maintain compatibility with API levels
514below 35.
515
Elliott Hughesbb1cb032023-01-18 23:26:58 +0000516You can read more about relative relocations
517and their long and complicated history at
518https://maskray.me/blog/2021-10-31-relative-relocations-and-relr.
Yabin Cuibd73dcc2023-09-05 14:03:06 -0700519
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000520
Yabin Cuibd73dcc2023-09-05 14:03:06 -0700521## No more sentinels in .preinit_array/.init_array/.fini_array sections of executables (in All API levels)
522
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000523In Android <= API level 34 and NDK <= r26, Android used sentinels in the
524`.preinit_array`/`.init_array`/`.fini_array` sections of executables to locate
525the start and end of these arrays. When building with LTO, the function pointers
526in the arrays can be reordered, making sentinels no longer work. This prevents
527constructors for global C++ variables from being called in static executables
528when using LTO.
Yabin Cuibd73dcc2023-09-05 14:03:06 -0700529
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000530To fix this, in Android >= API level 35 and NDK >= r27, we removed sentinels
531and switched to using symbols inserted by LLD (like `__init_array_start`,
532`__init_array_end`) to locate the arrays. This also avoids the need for an
533empty section when there are no corresponding functions.
Yabin Cuibd73dcc2023-09-05 14:03:06 -0700534
Elliott Hughesa5c8bcc2024-03-28 00:51:13 +0000535For dynamic executables, we kept sentinel support in `crtbegin_dynamic.o` and
536`libc.so`. This ensures that executables built with newer `crtbegin_dynamic.o`
537(in NDK >= r27) work with older `libc.so` (in Android <= API level 34), and
538vice versa.
Elliott Hughesd46acf62025-01-15 12:06:13 -0800539
540
541## Only files named `lib*.so` are copied by `extractNativeLibs` (Enforced for API level <= 35)
542
543Until API level 36, PackageManager would only install files whose names match
544the glob `lib*.so` when extracting native libraries _for non-debuggable apps_.
545This was especially confusing (and hard to debug) because the restriction did
546_not_ apply if your app was debuggable. To be compatible with all API levels,
547always give files that need to be extracted a "lib" prefix and ".so" suffix,
548or avoid using `extractNativeLibs`.
Elliott Hughesfae58f52025-03-25 08:03:49 -0700549
550
551## The LD_DEBUG environment variable.
552
553On devices running API level 37 or later you can also use the `LD_DEBUG`
554environment variable when running a stand-alone executable such as a unit test.
555The syntax is broadly similar to glibc, and you can get help for the specific
556version of Android you're on by using `LD_DEBUG=help`.
557You can also enable everything by using `LD_DEBUG=all`.
558
559(Older versions of Android also supported `LD_DEBUG`,
560but used integers instead of strings.
561The meaning of those integers varied by release,
562and some releases compiled support for `LD_DEBUG` out of released builds,
563so the best advice is either "look at the corresponding source" or
564"start with `1` and keep increasing the number until you see what you want,
565or see no change in output from the previous value".)