Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Android support to gimli #415

Merged
merged 1 commit into from
Apr 26, 2021

Conversation

kjvalencik
Copy link
Contributor

@kjvalencik kjvalencik commented Apr 9, 2021

Depends on rust-lang/libc#2144
Resolves #351

dl_iterate_phdr was added in API Version 21; however, it's very old and I couldn't figure out how to build for it. I believe this will fail to link on API version 20 and below, but I'm not certain.

Any suggestions for the best way to handle it? Any suggestions for testing would be appreciated as well.

bors added a commit to rust-lang/libc that referenced this pull request Apr 12, 2021
Add dl_iterate_phdr to Android

Adds the `dl_iterate_phdr` function for Android targets. This is required for Android support in `gimli` and by proxy, `backtrace`.

I tested this in [`backtrace`](rust-lang/backtrace-rs#415) both in an i686 emulator and a physical arm64 device.

This API is only available on Version 21+. I'm not sure how that's typically handled in `libc`, so I added a doc comment.

Let me know what else is needed!
@kjvalencik
Copy link
Contributor Author

@alexcrichton Do you have any suggestions for handling the Android API version check? Options that I've thought of:

  • Require users to enable it with a feature flag
  • Port the existing check that uses cc in a build.rs
  • Don't use libc and instead load the symbol dynamically with libloading

@alexcrichton
Copy link
Member

Thanks for this! I'd probably say that the second option, using the same build.rs check to see what the compiler api level reports, is the best for now. That way folks who want backwards-compat can get it and otherwise we'll automatically enable backtraces for more recent android versions (which should be basically all of them)

@kjvalencik kjvalencik force-pushed the kv/android-gimli branch 3 times, most recently from 4d8cc0d to b75d868 Compare April 16, 2021 19:46
@alexcrichton
Copy link
Member

Looks great to me! I'm happy to merge once libc is published with the update.

@kjvalencik kjvalencik changed the title wip: Android support in gimli Add Android support to gimli Apr 26, 2021
@kjvalencik kjvalencik marked this pull request as ready for review April 26, 2021 14:53
@kjvalencik
Copy link
Contributor Author

Thanks again for the review @alexcrichton. libc has been published with the required function and the PR has been updated to point at the latest version.

@alexcrichton alexcrichton merged commit 6b13969 into rust-lang:master Apr 26, 2021
@alexcrichton
Copy link
Member

Looks great to me, thanks!

@s1341
Copy link

s1341 commented Apr 27, 2021

Awesome. Can we get a release so we can integrate this?

@alexcrichton
Copy link
Member

Sure thing, done now!

@s1341
Copy link

s1341 commented Apr 28, 2021

Thanks!

@s1341
Copy link

s1341 commented Apr 28, 2021

Confirmed working! Thanks all!

MarijnS95 added a commit to rust-mobile/xbuild that referenced this pull request Jan 7, 2025
We haven't set the SDK/API level via the `__ANDROID_API__` define for
a very long time and so far got away with it.  However, while debugging
why `backtrace` (and by extension Rust `std` which reuses that crate)
wasn't generating symbolicated stacktraces in `panic_log`, and why
`findshlibs` wasn't providing the list of loaded libraries to `sentry`,
we found that both rely on expanding the `__ANDROID_API__` define
via compiling a small C file via `cc` to make the code conditional on
SDK/API >= 21:

gimli-rs/findshlibs#65
rust-lang/backtrace-rs#415

(It would have been lovely if these crates emitted a `cargo:warning`
when the define wasn't set at all, indicating an "incomplete"
cross-compiler setup, and/or looked at the runtime Android API version
via something like rust-mobile/ndk#479.)

Note that `backtrace 0.3.74` / Rust 1.82
(rust-lang/rust@0763a3a) no longer rely on
this because Rust 1.82 bumped the minimum SDK/API level to 21:
rust-lang/backtrace-rs#656

We could set this define directly, or rely on `clang` to set it
for us by appending the SDK/API level to the target triple, of
the form `<arch>-linux-android<sdk level>`.  The latter is more
common. Keep in mind that the `cc` crate adds an unversioned
`--target=<arch>-linux-android` to the command line arguments as well,
but clang seems to deduplicate them (or look at the latter `--target`
which contains our version).

Note that this effectively reverts
32efed6 because
we must now always pass the SDK level via the triple again, even if the
host also happens to be Android with the same architecture.
MarijnS95 added a commit to Traverse-Research/xbuild that referenced this pull request Jan 7, 2025
We haven't set the SDK/API level via the `__ANDROID_API__` define for
a very long time and so far got away with it.  However, while debugging
why `backtrace` (and by extension Rust `std` which reuses that crate)
wasn't generating symbolicated stacktraces in `panic_log`, and why
`findshlibs` wasn't providing the list of loaded libraries to `sentry`,
we found that both rely on expanding the `__ANDROID_API__` define
via compiling a small C file via `cc` to make the code conditional on
SDK/API >= 21:

gimli-rs/findshlibs#65
rust-lang/backtrace-rs#415

(It would have been lovely if these crates emitted a `cargo:warning`
when the define wasn't set at all, indicating an "incomplete"
cross-compiler setup, and/or looked at the runtime Android API version
via something like rust-mobile/ndk#479.)

Note that `backtrace 0.3.74` / Rust 1.82
(rust-lang/rust@0763a3a) no longer rely on
this because Rust 1.82 bumped the minimum SDK/API level to 21:
rust-lang/backtrace-rs#656

We could set this define directly, or rely on `clang` to set it
for us by appending the SDK/API level to the target triple, of
the form `<arch>-linux-android<sdk level>`.  The latter is more
common. Keep in mind that the `cc` crate adds an unversioned
`--target=<arch>-linux-android` to the command line arguments as well,
but clang seems to deduplicate them (or look at the latter `--target`
which contains our version).

Note that this effectively reverts
rust-mobile@32efed6 because
we must now always pass the SDK level via the triple again, even if the
host also happens to be Android with the same architecture.
MarijnS95 added a commit to Traverse-Research/xbuild that referenced this pull request Jan 9, 2025
We haven't set the SDK/API level via the `__ANDROID_API__` define for
a very long time and so far got away with it.  However, while debugging
why `backtrace` (and by extension Rust `std` which reuses that crate)
wasn't generating symbolicated stacktraces in `panic_log`, and why
`findshlibs` wasn't providing the list of loaded libraries to `sentry`,
we found that both rely on expanding the `__ANDROID_API__` define
via compiling a small C file via `cc` to make the code conditional on
SDK/API >= 21:

gimli-rs/findshlibs#65
rust-lang/backtrace-rs#415

(It would have been lovely if these crates emitted a `cargo:warning`
when the define wasn't set at all, indicating an "incomplete"
cross-compiler setup, and/or looked at the runtime Android API version
via something like rust-mobile/ndk#479.)

Note that `backtrace 0.3.74` / Rust 1.82
(rust-lang/rust@0763a3a) no longer rely on
this because Rust 1.82 bumped the minimum SDK/API level to 21:
rust-lang/backtrace-rs#656

We could set this define directly, or rely on `clang` to set it
for us by appending the SDK/API level to the target triple, of
the form `<arch>-linux-android<sdk level>`.  The latter is more
common. Keep in mind that the `cc` crate adds an unversioned
`--target=<arch>-linux-android` to the command line arguments as well,
but clang seems to deduplicate them (or look at the latter `--target`
which contains our version).

Note that this effectively reverts
rust-mobile@32efed6 because
we must now always pass the SDK level via the triple again, even if the
host also happens to be Android with the same architecture.
MarijnS95 added a commit to rust-mobile/xbuild that referenced this pull request Jan 9, 2025
We haven't set the SDK/API level via the `__ANDROID_API__` define for
a very long time and so far got away with it.  However, while debugging
why `backtrace` (and by extension Rust `std` which reuses that crate)
wasn't generating symbolicated stacktraces in `panic_log`, and why
`findshlibs` wasn't providing the list of loaded libraries to `sentry`,
we found that both rely on expanding the `__ANDROID_API__` define
via compiling a small C file via `cc` to make the code conditional on
SDK/API >= 21:

gimli-rs/findshlibs#65
rust-lang/backtrace-rs#415

(It would have been lovely if these crates emitted a `cargo:warning`
when the define wasn't set at all, indicating an "incomplete"
cross-compiler setup, and/or looked at the runtime Android API version
via something like rust-mobile/ndk#479.)

Note that `backtrace 0.3.74` / Rust 1.82
(rust-lang/rust@0763a3a) no longer rely on
this because Rust 1.82 bumped the minimum SDK/API level to 21:
rust-lang/backtrace-rs#656

We could set this define directly, or rely on `clang` to set it
for us by appending the SDK/API level to the target triple, of
the form `<arch>-linux-android<sdk level>`.  The latter is more
common. Keep in mind that the `cc` crate adds an unversioned
`--target=<arch>-linux-android` to the command line arguments as well,
but clang seems to deduplicate them (or look at the latter `--target`
which contains our version).

Note that this effectively reverts
32efed6 because
we must now always pass the SDK level via the triple again, even if the
host also happens to be Android with the same architecture.
MarijnS95 added a commit to Traverse-Research/xbuild that referenced this pull request Jan 9, 2025
We haven't set the SDK/API level via the `__ANDROID_API__` define for
a very long time and so far got away with it.  However, while debugging
why `backtrace` (and by extension Rust `std` which reuses that crate)
wasn't generating symbolicated stacktraces in `panic_log`, and why
`findshlibs` wasn't providing the list of loaded libraries to `sentry`,
we found that both rely on expanding the `__ANDROID_API__` define
via compiling a small C file via `cc` to make the code conditional on
SDK/API >= 21:

gimli-rs/findshlibs#65
rust-lang/backtrace-rs#415

(It would have been lovely if these crates emitted a `cargo:warning`
when the define wasn't set at all, indicating an "incomplete"
cross-compiler setup, and/or looked at the runtime Android API version
via something like rust-mobile/ndk#479.)

Note that `backtrace 0.3.74` / Rust 1.82
(rust-lang/rust@0763a3a) no longer rely on
this because Rust 1.82 bumped the minimum SDK/API level to 21:
rust-lang/backtrace-rs#656

We could set this define directly, or rely on `clang` to set it
for us by appending the SDK/API level to the target triple, of
the form `<arch>-linux-android<sdk level>`.  The latter is more
common. Keep in mind that the `cc` crate adds an unversioned
`--target=<arch>-linux-android` to the command line arguments as well,
but clang seems to deduplicate them (or look at the latter `--target`
which contains our version).

Note that this effectively reverts
rust-mobile@32efed6 because
we must now always pass the SDK level via the triple again, even if the
host also happens to be Android with the same architecture.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gimli doesn't support Android
3 participants