@@ -224,6 +224,32 @@ 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
235+ fromRust (LLVMRustCompressionKind Kind) {
236+ switch (Kind) {
237+ case LLVMRustCompressionKind::None:
238+ return llvm::DebugCompressionType::None;
239+ case LLVMRustCompressionKind::Zlib:
240+ if (!llvm::compression::zlib::isAvailable ()) {
241+ report_fatal_error (" LLVMRustCompressionKind::Zlib not available" );
242+ }
243+ return llvm::DebugCompressionType::Zlib;
244+ case LLVMRustCompressionKind::Zstd:
245+ if (!llvm::compression::zstd::isAvailable ()) {
246+ report_fatal_error (" LLVMRustCompressionKind::Zstd not available" );
247+ }
248+ return llvm::DebugCompressionType::Zstd;
249+ }
250+ report_fatal_error (" bad LLVMRustCompressionKind" );
251+ }
252+
227253extern " C" void LLVMRustPrintTargetCPUs (LLVMTargetMachineRef TM,
228254 RustStringRef OutStr) {
229255 ArrayRef<SubtargetSubTypeKV> CPUTable =
@@ -271,7 +297,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
271297 bool TrapUnreachable, bool Singlethread, bool VerboseAsm,
272298 bool EmitStackSizeSection, bool RelaxELFRelocations, bool UseInitArray,
273299 const char *SplitDwarfFile, const char *OutputObjFile,
274- const char *DebugInfoCompression, bool UseEmulatedTls, bool UseWasmEH) {
300+ LLVMRustCompressionKind DebugInfoCompression, bool UseEmulatedTls,
301+ bool UseWasmEH) {
275302
276303 auto OptLevel = fromRust (RustOptLevel);
277304 auto RM = fromRust (RustReloc);
@@ -307,16 +334,10 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
307334 if (OutputObjFile) {
308335 Options.ObjectFilenameForDebug = OutputObjFile;
309336 }
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-
337+ // To avoid fatal errors, make sure the Rust-side code only passes a
338+ // compression kind that is known to be supported by this build of LLVM, via
339+ // `LLVMRustLLVMHasZlibCompression` and `LLVMRustLLVMHasZstdCompression`.
340+ Options.MCOptions .CompressDebugSections = fromRust (DebugInfoCompression);
320341 Options.MCOptions .X86RelaxRelocations = RelaxELFRelocations;
321342 Options.UseInitArray = UseInitArray;
322343 Options.EmulatedTLS = UseEmulatedTls;
0 commit comments