Skip to content

Commit

Permalink
More robust OS detection for riscv feature detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Shnatsel committed Nov 9, 2024
1 parent 4ccbc72 commit bdf0413
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cargo-auditable/src/object_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn create_object_file(
Architecture::Riscv32 | Architecture::Riscv64 => {
// Source: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/079772828bd10933d34121117a222b4cc0ee2200/riscv-elf.adoc
let mut e_flags: u32 = 0x0;
let features = riscv_features(target_triple);
let features = riscv_features(target_triple, info);
// Check if compressed is enabled
if features.contains('c') {
e_flags |= elf::EF_RISCV_RVC;
Expand Down Expand Up @@ -187,7 +187,10 @@ fn create_object_file(
// This function was not present in the original rustc code, which simply used
// `sess.target.options.features`
// We do not have access to compiler internals, so we have to reimplement this function.
fn riscv_features(target_triple: &str) -> String {
// And `rustc --print=cfg` doesn't expose some of the features we care about,
// specifically the 'd' and 'f' features.
// Hence this function, which is not as robust as I would like.
fn riscv_features(target_triple: &str, info: &RustcTargetInfo) -> String {
let arch = target_triple.split('-').next().unwrap();
assert_eq!(&arch[..5], "riscv");
let mut extensions = arch[7..].to_owned();
Expand All @@ -197,8 +200,9 @@ fn riscv_features(target_triple: &str) -> String {
// Most but not all riscv targets declare target features.
// A notable exception is `riscv64-linux-android`.
// We assume that all Linux-capable targets are -gc.
if target_triple.contains("linux") {
extensions.push_str("imadfc");
match info["target_os"].as_str() {
"linux" | "android" => extensions.push_str("imadfc"),
_ => (),
}
extensions
}
Expand Down

0 comments on commit bdf0413

Please sign in to comment.