Set path of the compile unit to the source directory

As part of the effort to implement split dwarf debug info, we ended up
setting the compile unit location to the output directory rather than
the source directory. Furthermore, it seems like we failed to remap the
prefixes for this as well!

The desired behaviour is to instead set the `DW_AT_GNU_dwo_name` to a
path relative to compiler's working directory. This still allows
debuggers to find the split dwarf files, while not changing the
behaviour of the code that is compiling with regular debug info, and not
changing the compiler's behaviour with regards to reproducibility.

Fixes #82074
This commit is contained in:
Simonas Kazlauskas 2021-02-14 17:12:14 +02:00
parent 3158857297
commit 16c71886c9
4 changed files with 13 additions and 21 deletions

View file

@ -93,7 +93,7 @@ pub fn create_informational_target_machine(sess: &Session) -> &'static mut llvm:
pub fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> &'static mut llvm::TargetMachine { pub fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> &'static mut llvm::TargetMachine {
let split_dwarf_file = if tcx.sess.target_can_use_split_dwarf() { let split_dwarf_file = if tcx.sess.target_can_use_split_dwarf() {
tcx.output_filenames(LOCAL_CRATE) tcx.output_filenames(LOCAL_CRATE)
.split_dwarf_filename(tcx.sess.split_debuginfo(), Some(mod_name)) .split_dwarf_path(tcx.sess.split_debuginfo(), Some(mod_name))
} else { } else {
None None
}; };

View file

@ -979,7 +979,7 @@ pub fn compile_unit_metadata(
// The OSX linker has an idiosyncrasy where it will ignore some debuginfo // The OSX linker has an idiosyncrasy where it will ignore some debuginfo
// if multiple object files with the same `DW_AT_name` are linked together. // if multiple object files with the same `DW_AT_name` are linked together.
// As a workaround we generate unique names for each object file. Those do // As a workaround we generate unique names for each object file. Those do
// not correspond to an actual source file but that should be harmless. // not correspond to an actual source file but that is harmless.
if tcx.sess.target.is_like_osx { if tcx.sess.target.is_like_osx {
name_in_debuginfo.push("@"); name_in_debuginfo.push("@");
name_in_debuginfo.push(codegen_unit_name); name_in_debuginfo.push(codegen_unit_name);
@ -992,17 +992,17 @@ pub fn compile_unit_metadata(
let producer = format!("clang LLVM ({})", rustc_producer); let producer = format!("clang LLVM ({})", rustc_producer);
let name_in_debuginfo = name_in_debuginfo.to_string_lossy(); let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
let work_dir = tcx.sess.working_dir.0.to_string_lossy();
let flags = "\0"; let flags = "\0";
let out_dir = &tcx.output_filenames(LOCAL_CRATE).out_directory; let out_dir = &tcx.output_filenames(LOCAL_CRATE).out_directory;
let split_name = if tcx.sess.target_can_use_split_dwarf() { let split_name = if tcx.sess.target_can_use_split_dwarf() {
tcx.output_filenames(LOCAL_CRATE) tcx.output_filenames(LOCAL_CRATE)
.split_dwarf_filename(tcx.sess.split_debuginfo(), Some(codegen_unit_name)) .split_dwarf_path(tcx.sess.split_debuginfo(), Some(codegen_unit_name))
.map(|f| out_dir.join(f))
} else { } else {
None None
} }
.unwrap_or_default(); .unwrap_or_default();
let out_dir = out_dir.to_str().unwrap();
let split_name = split_name.to_str().unwrap(); let split_name = split_name.to_str().unwrap();
// FIXME(#60020): // FIXME(#60020):
@ -1024,12 +1024,12 @@ pub fn compile_unit_metadata(
assert!(tcx.sess.opts.debuginfo != DebugInfo::None); assert!(tcx.sess.opts.debuginfo != DebugInfo::None);
unsafe { unsafe {
let file_metadata = llvm::LLVMRustDIBuilderCreateFile( let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile(
debug_context.builder, debug_context.builder,
name_in_debuginfo.as_ptr().cast(), name_in_debuginfo.as_ptr().cast(),
name_in_debuginfo.len(), name_in_debuginfo.len(),
out_dir.as_ptr().cast(), work_dir.as_ptr().cast(),
out_dir.len(), work_dir.len(),
llvm::ChecksumKind::None, llvm::ChecksumKind::None,
ptr::null(), ptr::null(),
0, 0,
@ -1038,12 +1038,15 @@ pub fn compile_unit_metadata(
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit( let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
debug_context.builder, debug_context.builder,
DW_LANG_RUST, DW_LANG_RUST,
file_metadata, compile_unit_file,
producer.as_ptr().cast(), producer.as_ptr().cast(),
producer.len(), producer.len(),
tcx.sess.opts.optimize != config::OptLevel::No, tcx.sess.opts.optimize != config::OptLevel::No,
flags.as_ptr().cast(), flags.as_ptr().cast(),
0, 0,
// NB: this doesn't actually have any perceptible effect, it seems. LLVM will instead
// put the path supplied to `MCSplitDwarfFile` into the debug info of the final
// output(s).
split_name.as_ptr().cast(), split_name.as_ptr().cast(),
split_name.len(), split_name.len(),
kind, kind,

View file

@ -288,7 +288,7 @@ impl TargetMachineFactoryConfig {
module_name: &str, module_name: &str,
) -> TargetMachineFactoryConfig { ) -> TargetMachineFactoryConfig {
let split_dwarf_file = if cgcx.target_can_use_split_dwarf { let split_dwarf_file = if cgcx.target_can_use_split_dwarf {
cgcx.output_filenames.split_dwarf_filename(cgcx.split_debuginfo, Some(module_name)) cgcx.output_filenames.split_dwarf_path(cgcx.split_debuginfo, Some(module_name))
} else { } else {
None None
}; };

View file

@ -667,17 +667,6 @@ impl OutputFilenames {
path path
} }
/// Returns the name of the Split DWARF file - this can differ depending on which Split DWARF
/// mode is being used, which is the logic that this function is intended to encapsulate.
pub fn split_dwarf_filename(
&self,
split_debuginfo_kind: SplitDebuginfo,
cgu_name: Option<&str>,
) -> Option<PathBuf> {
self.split_dwarf_path(split_debuginfo_kind, cgu_name)
.map(|path| path.strip_prefix(&self.out_directory).unwrap_or(&path).to_path_buf())
}
/// Returns the path for the Split DWARF file - this can differ depending on which Split DWARF /// Returns the path for the Split DWARF file - this can differ depending on which Split DWARF
/// mode is being used, which is the logic that this function is intended to encapsulate. /// mode is being used, which is the logic that this function is intended to encapsulate.
pub fn split_dwarf_path( pub fn split_dwarf_path(