Skip to content

Commit 98afc30

Browse files
committed
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 rust-lang#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 rust-lang#90326 by itself yet, as .llvmbc will need to be handled separately.
1 parent 48fc7d9 commit 98afc30

File tree

2 files changed

+0
-44
lines changed

2 files changed

+0
-44
lines changed

src/base.rs

-39
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ use gccjit::{
77
GlobalKind,
88
};
99
use rustc_middle::dep_graph;
10-
use rustc_middle::middle::exported_symbols;
1110
use rustc_middle::ty::TyCtxt;
1211
use rustc_middle::mir::mono::Linkage;
1312
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
1413
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
1514
use rustc_codegen_ssa::mono_item::MonoItemExt;
1615
use rustc_codegen_ssa::traits::DebugInfoMethods;
17-
use rustc_metadata::EncodedMetadata;
1816
use rustc_session::config::DebugInfo;
1917
use rustc_span::Symbol;
2018

@@ -132,40 +130,3 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (Modul
132130

133131
(module, cost)
134132
}
135-
136-
pub fn write_compressed_metadata<'tcx>(tcx: TyCtxt<'tcx>, metadata: &EncodedMetadata, gcc_module: &mut GccContext) {
137-
use snap::write::FrameEncoder;
138-
use std::io::Write;
139-
140-
// Historical note:
141-
//
142-
// When using link.exe it was seen that the section name `.note.rustc`
143-
// was getting shortened to `.note.ru`, and according to the PE and COFF
144-
// specification:
145-
//
146-
// > Executable images do not use a string table and do not support
147-
// > section names longer than 8 characters
148-
//
149-
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
150-
//
151-
// As a result, we choose a slightly shorter name! As to why
152-
// `.note.rustc` works on MinGW, see
153-
// https://github.com/llvm/llvm-project/blob/llvmorg-12.0.0/lld/COFF/Writer.cpp#L1190-L1197
154-
let section_name = if tcx.sess.target.is_like_osx { "__DATA,.rustc" } else { ".rustc" };
155-
156-
let context = &gcc_module.context;
157-
let mut compressed = rustc_metadata::METADATA_HEADER.to_vec();
158-
FrameEncoder::new(&mut compressed).write_all(&metadata.raw_data()).unwrap();
159-
160-
let name = exported_symbols::metadata_symbol_name(tcx);
161-
let typ = context.new_array_type(None, context.new_type::<u8>(), compressed.len() as i32);
162-
let global = context.new_global(None, GlobalKind::Exported, typ, name);
163-
global.global_set_initializer(&compressed);
164-
global.set_link_section(section_name);
165-
166-
// Also generate a .section directive to force no
167-
// flags, at least for ELF outputs, so that the
168-
// metadata doesn't get loaded into memory.
169-
let directive = format!(".section {}", section_name);
170-
context.add_top_level_asm(None, &directive);
171-
}

src/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ extern crate rustc_session;
2222
extern crate rustc_span;
2323
extern crate rustc_symbol_mangling;
2424
extern crate rustc_target;
25-
extern crate snap;
2625

2726
// This prevents duplicating functions and statics that are already part of the host rustc process.
2827
#[allow(unused_extern_crates)]
@@ -128,10 +127,6 @@ impl ExtraBackendMethods for GccCodegenBackend {
128127
}
129128
}
130129

131-
fn write_compressed_metadata<'tcx>(&self, tcx: TyCtxt<'tcx>, metadata: &EncodedMetadata, gcc_module: &mut Self::Module) {
132-
base::write_compressed_metadata(tcx, metadata, gcc_module)
133-
}
134-
135130
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, mods: &mut Self::Module, module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) {
136131
unsafe { allocator::codegen(tcx, mods, module_name, kind, has_alloc_error_handler) }
137132
}

0 commit comments

Comments
 (0)