Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass name of object file to LLVM so it can correctly emit S_OBJNAME in pdb files on Windows #115704

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_llvm/src/back/owned_target_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl OwnedTargetMachine {
relax_elf_relocations: bool,
use_init_array: bool,
split_dwarf_file: &CStr,
output_obj_file: &CStr,
debug_info_compression: &CStr,
force_emulated_tls: bool,
args_cstr_buff: &[u8],
Expand Down Expand Up @@ -68,6 +69,7 @@ impl OwnedTargetMachine {
relax_elf_relocations,
use_init_array,
split_dwarf_file.as_ptr(),
output_obj_file.as_ptr(),
debug_info_compression.as_ptr(),
force_emulated_tls,
args_cstr_buff.as_ptr() as *const c_char,
Expand Down
19 changes: 14 additions & 5 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn write_output_file<'ll>(
}

pub fn create_informational_target_machine(sess: &Session) -> OwnedTargetMachine {
let config = TargetMachineFactoryConfig { split_dwarf_file: None };
let config = TargetMachineFactoryConfig { split_dwarf_file: None, output_obj_file: None };
// Can't use query system here quite yet because this function is invoked before the query
// system/tcx is set up.
let features = llvm_util::global_llvm_features(sess, false);
Expand All @@ -118,7 +118,11 @@ pub fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> OwnedTargetMach
} else {
None
};
let config = TargetMachineFactoryConfig { split_dwarf_file };

let output_obj_file =
Some(tcx.output_filenames(()).temp_path(OutputType::Object, Some(mod_name)));
let config = TargetMachineFactoryConfig { split_dwarf_file, output_obj_file };

target_machine_factory(
&tcx.sess,
tcx.backend_optimization_level(()),
Expand Down Expand Up @@ -256,9 +260,13 @@ pub fn target_machine_factory(
let debuginfo_compression = SmallCStr::new(&debuginfo_compression);

Arc::new(move |config: TargetMachineFactoryConfig| {
let split_dwarf_file =
path_mapping.map_prefix(config.split_dwarf_file.unwrap_or_default()).0;
let split_dwarf_file = CString::new(split_dwarf_file.to_str().unwrap()).unwrap();
let path_to_cstring_helper = |path: Option<PathBuf>| -> CString {
let path = path_mapping.map_prefix(path.unwrap_or_default()).0;
CString::new(path.to_str().unwrap()).unwrap()
};

let split_dwarf_file = path_to_cstring_helper(config.split_dwarf_file);
let output_obj_file = path_to_cstring_helper(config.output_obj_file);

OwnedTargetMachine::new(
&triple,
Expand All @@ -279,6 +287,7 @@ pub fn target_machine_factory(
relax_elf_relocations,
use_init_array,
&split_dwarf_file,
&output_obj_file,
&debuginfo_compression,
force_emulated_tls,
&args_cstr_buff,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,7 @@ extern "C" {
RelaxELFRelocations: bool,
UseInitArray: bool,
SplitDwarfFile: *const c_char,
OutputObjFile: *const c_char,
DebugInfoCompression: *const c_char,
ForceEmulatedTls: bool,
ArgsCstrBuff: *const c_char,
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ pub struct TargetMachineFactoryConfig {
/// so the path to the dwarf object has to be provided when we create the target machine.
/// This can be ignored by backends which do not need it for their Split DWARF support.
pub split_dwarf_file: Option<PathBuf>,

/// The name of the output object file. Used for setting OutputFilenames in target options
/// so that LLVM can emit the CodeView S_OBJNAME record in pdb files
pub output_obj_file: Option<PathBuf>,
}

impl TargetMachineFactoryConfig {
Expand All @@ -302,7 +306,10 @@ impl TargetMachineFactoryConfig {
} else {
None
};
TargetMachineFactoryConfig { split_dwarf_file }

let output_obj_file =
Some(cgcx.output_filenames.temp_path(OutputType::Object, Some(module_name)));
TargetMachineFactoryConfig { split_dwarf_file, output_obj_file }
}
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
bool RelaxELFRelocations,
bool UseInitArray,
const char *SplitDwarfFile,
const char *OutputObjFile,
const char *DebugInfoCompression,
bool ForceEmulatedTls,
const char *ArgsCstrBuff, size_t ArgsCstrBuffLen) {
Expand Down Expand Up @@ -448,6 +449,9 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
if (SplitDwarfFile) {
Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
}
if (OutputObjFile) {
Options.ObjectFilenameForDebug = OutputObjFile;
}
#if LLVM_VERSION_GE(16, 0)
if (!strcmp("zlib", DebugInfoCompression) && llvm::compression::zlib::isAvailable()) {
Options.CompressDebugSections = DebugCompressionType::Zlib;
Expand Down