Skip to content

Commit 278e02a

Browse files
authored
Rollup merge of rust-lang#106797 - FawazTirmizi:dev/issues/104284, r=bjorn3
riscv: Fix ELF header flags The previous version added both `EF_RISCV_FLOAT_ABI_DOUBLE` and `EF_RISCV_RVC` if the "D" extension was enabled on riscv64 targets. riscv32 targets were not accounted for. This patch changes this so that: - Only add `EF_RISCV_RVC` if the "C" extension is enabled - Add `EF_RISCV_FLOAT_ABI_SINGLE` if the "F" extension is enabled and the "D" extension is not - Add these ELF flags for riscv32 as well Fixes rust-lang#104284 r? rust-lang/risc-v
2 parents e0f6840 + 138a1d2 commit 278e02a

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,23 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static
165165
};
166166
e_flags
167167
}
168-
Architecture::Riscv64 if sess.target.options.features.contains("+d") => {
169-
// copied from `riscv64-linux-gnu-gcc foo.c -c`, note though
170-
// that the `+d` target feature represents whether the double
171-
// float abi is enabled.
172-
let e_flags = elf::EF_RISCV_RVC | elf::EF_RISCV_FLOAT_ABI_DOUBLE;
168+
Architecture::Riscv32 | Architecture::Riscv64 => {
169+
// Source: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/079772828bd10933d34121117a222b4cc0ee2200/riscv-elf.adoc
170+
let mut e_flags: u32 = 0x0;
171+
let features = &sess.target.options.features;
172+
// Check if compressed is enabled
173+
if features.contains("+c") {
174+
e_flags |= elf::EF_RISCV_RVC;
175+
}
176+
177+
// Select the appropriate floating-point ABI
178+
if features.contains("+d") {
179+
e_flags |= elf::EF_RISCV_FLOAT_ABI_DOUBLE;
180+
} else if features.contains("+f") {
181+
e_flags |= elf::EF_RISCV_FLOAT_ABI_SINGLE;
182+
} else {
183+
e_flags |= elf::EF_RISCV_FLOAT_ABI_SOFT;
184+
}
173185
e_flags
174186
}
175187
_ => 0,

0 commit comments

Comments
 (0)