@@ -224,6 +224,31 @@ static FloatABI::ABIType fromRust(LLVMRustFloatABI RustFloatAbi) {
224224 report_fatal_error (" Bad FloatABI." );
225225}
226226
227+ // Must match the layout of `rustc_codegen_llvm::llvm::ffi::CompressionKind`.
228+ enum class LLVMRustCompressionKind {
229+ None = 0 ,
230+ Zlib = 1 ,
231+ Zstd = 2 ,
232+ };
233+
234+ static llvm::DebugCompressionType fromRust (LLVMRustCompressionKind Kind) {
235+ switch (Kind) {
236+ case LLVMRustCompressionKind::None:
237+ return llvm::DebugCompressionType::None;
238+ case LLVMRustCompressionKind::Zlib:
239+ if (!llvm::compression::zlib::isAvailable ()) {
240+ report_fatal_error (" LLVMRustCompressionKind::Zlib not available" );
241+ }
242+ return llvm::DebugCompressionType::Zlib;
243+ case LLVMRustCompressionKind::Zstd:
244+ if (!llvm::compression::zstd::isAvailable ()) {
245+ report_fatal_error (" LLVMRustCompressionKind::Zstd not available" );
246+ }
247+ return llvm::DebugCompressionType::Zstd;
248+ }
249+ report_fatal_error (" bad LLVMRustCompressionKind" );
250+ }
251+
227252extern " C" void LLVMRustPrintTargetCPUs (LLVMTargetMachineRef TM,
228253 RustStringRef OutStr) {
229254 ArrayRef<SubtargetSubTypeKV> CPUTable =
@@ -271,7 +296,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
271296 bool TrapUnreachable, bool Singlethread, bool VerboseAsm,
272297 bool EmitStackSizeSection, bool RelaxELFRelocations, bool UseInitArray,
273298 const char *SplitDwarfFile, const char *OutputObjFile,
274- const char *DebugInfoCompression, bool UseEmulatedTls, bool UseWasmEH) {
299+ LLVMRustCompressionKind DebugInfoCompression, bool UseEmulatedTls,
300+ bool UseWasmEH) {
275301
276302 auto OptLevel = fromRust (RustOptLevel);
277303 auto RM = fromRust (RustReloc);
@@ -307,16 +333,10 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
307333 if (OutputObjFile) {
308334 Options.ObjectFilenameForDebug = OutputObjFile;
309335 }
310- if (!strcmp (" zlib" , DebugInfoCompression) &&
311- llvm::compression::zlib::isAvailable ()) {
312- Options.MCOptions .CompressDebugSections = DebugCompressionType::Zlib;
313- } else if (!strcmp (" zstd" , DebugInfoCompression) &&
314- llvm::compression::zstd::isAvailable ()) {
315- Options.MCOptions .CompressDebugSections = DebugCompressionType::Zstd;
316- } else if (!strcmp (" none" , DebugInfoCompression)) {
317- Options.MCOptions .CompressDebugSections = DebugCompressionType::None;
318- }
319-
336+ // To avoid fatal errors, make sure the Rust-side code only passes a
337+ // compression kind that is known to be supported by this build of LLVM, via
338+ // `LLVMRustLLVMHasZlibCompression` and `LLVMRustLLVMHasZstdCompression`.
339+ Options.MCOptions .CompressDebugSections = fromRust (DebugInfoCompression);
320340 Options.MCOptions .X86RelaxRelocations = RelaxELFRelocations;
321341 Options.UseInitArray = UseInitArray;
322342 Options.EmulatedTLS = UseEmulatedTls;
0 commit comments