From ea7b20d8f279ea8c63b9a4b8e9129fce0e3c2b5d Mon Sep 17 00:00:00 2001 From: comex Date: Tue, 28 Jan 2014 00:05:33 -0500 Subject: [PATCH] Add appropriate LLVM module flags for debug info. Set "Dwarf Version" to 2 on OS X to avoid toolchain incompatibility, and set "Debug Info Version" to prevent debug info from being stripped from bitcode. Fixes #11352. --- src/librustc/lib/llvm.rs | 5 +++++ src/librustc/middle/trans/debuginfo.rs | 16 +++++++++++++++- src/rustllvm/RustWrapper.cpp | 8 ++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/librustc/lib/llvm.rs b/src/librustc/lib/llvm.rs index 3693b00951bcc..a9c1df71d079c 100644 --- a/src/librustc/lib/llvm.rs +++ b/src/librustc/lib/llvm.rs @@ -1493,6 +1493,11 @@ pub mod llvm { Dialect: c_uint) -> ValueRef; + pub static LLVMRustDebugMetadataVersion: u32; + + pub fn LLVMRustAddModuleFlag(M: ModuleRef, + name: *c_char, + value: u32); pub fn LLVMDIBuilderCreate(M: ModuleRef) -> DIBuilderRef; diff --git a/src/librustc/middle/trans/debuginfo.rs b/src/librustc/middle/trans/debuginfo.rs index c4fdc89567032..42f069e270530 100644 --- a/src/librustc/middle/trans/debuginfo.rs +++ b/src/librustc/middle/trans/debuginfo.rs @@ -149,7 +149,7 @@ use std::ptr; use std::sync::atomics; use std::vec; use syntax::codemap::{Span, Pos}; -use syntax::{ast, codemap, ast_util, ast_map, opt_vec}; +use syntax::{abi, ast, codemap, ast_util, ast_map, opt_vec}; use syntax::parse::token; use syntax::parse::token::special_idents; @@ -263,6 +263,20 @@ pub fn finalize(cx: @CrateContext) { unsafe { llvm::LLVMDIBuilderFinalize(DIB(cx)); llvm::LLVMDIBuilderDispose(DIB(cx)); + // Debuginfo generation in LLVM by default uses a higher + // version of dwarf than OS X currently understands. We can + // instruct LLVM to emit an older version of dwarf, however, + // for OS X to understand. For more info see #11352 + // This can be overridden using --llvm-opts -dwarf-version,N. + if cx.sess.targ_cfg.os == abi::OsMacos { + "Dwarf Version".with_c_str( + |s| llvm::LLVMRustAddModuleFlag(cx.llmod, s, 2)); + } + + // Prevent bitcode readers from deleting the debug info. + "Debug Info Version".with_c_str( + |s| llvm::LLVMRustAddModuleFlag(cx.llmod, s, + llvm::LLVMRustDebugMetadataVersion)); }; } diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index 280df8cb10f57..2107f7c39a20d 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -156,6 +156,14 @@ DIT unwrapDI(LLVMValueRef ref) { return DIT(ref ? unwrap(ref) : NULL); } +extern "C" const uint32_t LLVMRustDebugMetadataVersion = DEBUG_METADATA_VERSION; + +extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M, + const char *name, + uint32_t value) { + unwrap(M)->addModuleFlag(Module::Warning, name, value); +} + extern "C" DIBuilderRef LLVMDIBuilderCreate(LLVMModuleRef M) { return new DIBuilder(*unwrap(M)); }