Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta metadata object files. This switches the generation of compressed .rustc object files to use the object crate as well. These have slightly different requirements in that .rmeta should be completely excluded from any final compilation artifacts, while .rustc should be part of shared objects, but not loaded into memory. The primary motivation for this change is #90326: In LLVM 14, the current way of setting section flags (and in particular, preventing the setting of SHF_ALLOC) will no longer work. There are other ways we could work around this, but switching to the object crate seems like the most elegant, as we already use it for .rmeta, and as it makes this independent of the codegen backend. In particular, we don't need separate handling in codegen_llvm and codegen_gcc. codegen_cranelift should be able to reuse the implementation as well, though I have omitted that here, as it is not based on codegen_ssa. This change mostly extracts the existing code for .rmeta handling to allow using it for .rustc as well, and adjust the codegen infrastructure to handle the metadata object file separately: We no longer create a backend-specific module for it, and directly produce the compiled module instead. This does not fix #90326 by itself yet, as .llvmbc will need to be handled separately.
This commit is contained in:
parent
48fc7d9351
commit
98afc30b95
39
src/base.rs
39
src/base.rs
@ -7,14 +7,12 @@
|
||||
GlobalKind,
|
||||
};
|
||||
use rustc_middle::dep_graph;
|
||||
use rustc_middle::middle::exported_symbols;
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_middle::mir::mono::Linkage;
|
||||
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
|
||||
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
|
||||
use rustc_codegen_ssa::mono_item::MonoItemExt;
|
||||
use rustc_codegen_ssa::traits::DebugInfoMethods;
|
||||
use rustc_metadata::EncodedMetadata;
|
||||
use rustc_session::config::DebugInfo;
|
||||
use rustc_span::Symbol;
|
||||
|
||||
@ -132,40 +130,3 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: Symbol) -> ModuleCodegen<GccContext
|
||||
|
||||
(module, cost)
|
||||
}
|
||||
|
||||
pub fn write_compressed_metadata<'tcx>(tcx: TyCtxt<'tcx>, metadata: &EncodedMetadata, gcc_module: &mut GccContext) {
|
||||
use snap::write::FrameEncoder;
|
||||
use std::io::Write;
|
||||
|
||||
// Historical note:
|
||||
//
|
||||
// When using link.exe it was seen that the section name `.note.rustc`
|
||||
// was getting shortened to `.note.ru`, and according to the PE and COFF
|
||||
// specification:
|
||||
//
|
||||
// > Executable images do not use a string table and do not support
|
||||
// > section names longer than 8 characters
|
||||
//
|
||||
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
|
||||
//
|
||||
// As a result, we choose a slightly shorter name! As to why
|
||||
// `.note.rustc` works on MinGW, see
|
||||
// https://github.com/llvm/llvm-project/blob/llvmorg-12.0.0/lld/COFF/Writer.cpp#L1190-L1197
|
||||
let section_name = if tcx.sess.target.is_like_osx { "__DATA,.rustc" } else { ".rustc" };
|
||||
|
||||
let context = &gcc_module.context;
|
||||
let mut compressed = rustc_metadata::METADATA_HEADER.to_vec();
|
||||
FrameEncoder::new(&mut compressed).write_all(&metadata.raw_data()).unwrap();
|
||||
|
||||
let name = exported_symbols::metadata_symbol_name(tcx);
|
||||
let typ = context.new_array_type(None, context.new_type::<u8>(), compressed.len() as i32);
|
||||
let global = context.new_global(None, GlobalKind::Exported, typ, name);
|
||||
global.global_set_initializer(&compressed);
|
||||
global.set_link_section(section_name);
|
||||
|
||||
// Also generate a .section directive to force no
|
||||
// flags, at least for ELF outputs, so that the
|
||||
// metadata doesn't get loaded into memory.
|
||||
let directive = format!(".section {}", section_name);
|
||||
context.add_top_level_asm(None, &directive);
|
||||
}
|
||||
|
@ -22,7 +22,6 @@
|
||||
extern crate rustc_span;
|
||||
extern crate rustc_symbol_mangling;
|
||||
extern crate rustc_target;
|
||||
extern crate snap;
|
||||
|
||||
// This prevents duplicating functions and statics that are already part of the host rustc process.
|
||||
#[allow(unused_extern_crates)]
|
||||
@ -128,10 +127,6 @@ fn new_metadata<'tcx>(&self, _tcx: TyCtxt<'tcx>, _mod_name: &str) -> Self::Modul
|
||||
}
|
||||
}
|
||||
|
||||
fn write_compressed_metadata<'tcx>(&self, tcx: TyCtxt<'tcx>, metadata: &EncodedMetadata, gcc_module: &mut Self::Module) {
|
||||
base::write_compressed_metadata(tcx, metadata, gcc_module)
|
||||
}
|
||||
|
||||
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, mods: &mut Self::Module, module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) {
|
||||
unsafe { allocator::codegen(tcx, mods, module_name, kind, has_alloc_error_handler) }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user