1#![allow(internal_features)]
3#![allow(rustc::diagnostic_outside_of_impl)]
4#![allow(rustc::untranslatable_diagnostic)]
5#![cfg_attr(doc, recursion_limit = "256")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
7#![doc(rust_logo)]
8#![feature(assert_matches)]
9#![feature(box_patterns)]
10#![feature(debug_closure_helpers)]
11#![feature(file_buffered)]
12#![feature(if_let_guard)]
13#![feature(let_chains)]
14#![feature(negative_impls)]
15#![feature(rustdoc_internals)]
16#![feature(string_from_utf8_lossy_owned)]
17#![feature(trait_alias)]
18#![feature(try_blocks)]
19use std::collections::BTreeSet;
26use std::io;
27use std::path::{Path, PathBuf};
28use std::sync::Arc;
29
30use rustc_ast as ast;
31use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
32use rustc_data_structures::unord::UnordMap;
33use rustc_hir::CRATE_HIR_ID;
34use rustc_hir::def_id::CrateNum;
35use rustc_macros::{Decodable, Encodable, HashStable};
36use rustc_middle::dep_graph::WorkProduct;
37use rustc_middle::lint::LintLevelSource;
38use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
39use rustc_middle::middle::dependency_format::Dependencies;
40use rustc_middle::middle::exported_symbols::SymbolExportKind;
41use rustc_middle::ty::TyCtxt;
42use rustc_middle::util::Providers;
43use rustc_serialize::opaque::{FileEncoder, MemDecoder};
44use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
45use rustc_session::Session;
46use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
47use rustc_session::cstore::{self, CrateSource};
48use rustc_session::lint::Level;
49use rustc_session::lint::builtin::LINKER_MESSAGES;
50use rustc_session::utils::NativeLibKind;
51use rustc_span::Symbol;
52
53pub mod assert_module_sources;
54pub mod back;
55pub mod base;
56pub mod codegen_attrs;
57pub mod common;
58pub mod debuginfo;
59pub mod errors;
60pub mod meth;
61pub mod mir;
62pub mod mono_item;
63pub mod size_of_val;
64pub mod target_features;
65pub mod traits;
66
67rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
68
69pub struct ModuleCodegen<M> {
70 pub name: String,
77 pub module_llvm: M,
78 pub kind: ModuleKind,
79 pub thin_lto_buffer: Option<Vec<u8>>,
81}
82
83impl<M> ModuleCodegen<M> {
84 pub fn new_regular(name: impl Into<String>, module: M) -> Self {
85 Self {
86 name: name.into(),
87 module_llvm: module,
88 kind: ModuleKind::Regular,
89 thin_lto_buffer: None,
90 }
91 }
92
93 pub fn new_allocator(name: impl Into<String>, module: M) -> Self {
94 Self {
95 name: name.into(),
96 module_llvm: module,
97 kind: ModuleKind::Allocator,
98 thin_lto_buffer: None,
99 }
100 }
101
102 pub fn into_compiled_module(
103 self,
104 emit_obj: bool,
105 emit_dwarf_obj: bool,
106 emit_bc: bool,
107 emit_asm: bool,
108 emit_ir: bool,
109 outputs: &OutputFilenames,
110 ) -> CompiledModule {
111 let object = emit_obj.then(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
112 let dwarf_object = emit_dwarf_obj.then(|| outputs.temp_path_dwo(Some(&self.name)));
113 let bytecode = emit_bc.then(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
114 let assembly = emit_asm.then(|| outputs.temp_path(OutputType::Assembly, Some(&self.name)));
115 let llvm_ir =
116 emit_ir.then(|| outputs.temp_path(OutputType::LlvmAssembly, Some(&self.name)));
117
118 CompiledModule {
119 name: self.name.clone(),
120 kind: self.kind,
121 object,
122 dwarf_object,
123 bytecode,
124 assembly,
125 llvm_ir,
126 links_from_incr_cache: Vec::new(),
127 }
128 }
129}
130
131#[derive(Debug, Encodable, Decodable)]
132pub struct CompiledModule {
133 pub name: String,
134 pub kind: ModuleKind,
135 pub object: Option<PathBuf>,
136 pub dwarf_object: Option<PathBuf>,
137 pub bytecode: Option<PathBuf>,
138 pub assembly: Option<PathBuf>, pub llvm_ir: Option<PathBuf>, pub links_from_incr_cache: Vec<PathBuf>,
141}
142
143impl CompiledModule {
144 pub fn for_each_output(&self, mut emit: impl FnMut(&Path, OutputType)) {
146 if let Some(path) = self.object.as_deref() {
147 emit(path, OutputType::Object);
148 }
149 if let Some(path) = self.bytecode.as_deref() {
150 emit(path, OutputType::Bitcode);
151 }
152 if let Some(path) = self.llvm_ir.as_deref() {
153 emit(path, OutputType::LlvmAssembly);
154 }
155 if let Some(path) = self.assembly.as_deref() {
156 emit(path, OutputType::Assembly);
157 }
158 }
159}
160
161pub(crate) struct CachedModuleCodegen {
162 pub name: String,
163 pub source: WorkProduct,
164}
165
166#[derive(Copy, Clone, Debug, PartialEq, Encodable, Decodable)]
167pub enum ModuleKind {
168 Regular,
169 Metadata,
170 Allocator,
171}
172
173bitflags::bitflags! {
174 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
175 pub struct MemFlags: u8 {
176 const VOLATILE = 1 << 0;
177 const NONTEMPORAL = 1 << 1;
178 const UNALIGNED = 1 << 2;
179 }
180}
181
182#[derive(Clone, Debug, Encodable, Decodable, HashStable)]
183pub struct NativeLib {
184 pub kind: NativeLibKind,
185 pub name: Symbol,
186 pub filename: Option<Symbol>,
187 pub cfg: Option<ast::MetaItemInner>,
188 pub verbatim: bool,
189 pub dll_imports: Vec<cstore::DllImport>,
190}
191
192impl From<&cstore::NativeLib> for NativeLib {
193 fn from(lib: &cstore::NativeLib) -> Self {
194 NativeLib {
195 kind: lib.kind,
196 filename: lib.filename,
197 name: lib.name,
198 cfg: lib.cfg.clone(),
199 verbatim: lib.verbatim.unwrap_or(false),
200 dll_imports: lib.dll_imports.clone(),
201 }
202 }
203}
204
205#[derive(Debug, Encodable, Decodable)]
214pub struct CrateInfo {
215 pub target_cpu: String,
216 pub target_features: Vec<String>,
217 pub crate_types: Vec<CrateType>,
218 pub exported_symbols: UnordMap<CrateType, Vec<String>>,
219 pub linked_symbols: FxIndexMap<CrateType, Vec<(String, SymbolExportKind)>>,
220 pub local_crate_name: Symbol,
221 pub compiler_builtins: Option<CrateNum>,
222 pub profiler_runtime: Option<CrateNum>,
223 pub is_no_builtins: FxHashSet<CrateNum>,
224 pub native_libraries: FxIndexMap<CrateNum, Vec<NativeLib>>,
225 pub crate_name: UnordMap<CrateNum, Symbol>,
226 pub used_libraries: Vec<NativeLib>,
227 pub used_crate_source: UnordMap<CrateNum, Arc<CrateSource>>,
228 pub used_crates: Vec<CrateNum>,
229 pub dependency_formats: Arc<Dependencies>,
230 pub windows_subsystem: Option<String>,
231 pub natvis_debugger_visualizers: BTreeSet<DebuggerVisualizerFile>,
232 pub lint_levels: CodegenLintLevels,
233}
234
235#[derive(Encodable, Decodable)]
236pub struct CodegenResults {
237 pub modules: Vec<CompiledModule>,
238 pub allocator_module: Option<CompiledModule>,
239 pub metadata_module: Option<CompiledModule>,
240 pub metadata: rustc_metadata::EncodedMetadata,
241 pub crate_info: CrateInfo,
242}
243
244pub enum CodegenErrors {
245 WrongFileType,
246 EmptyVersionNumber,
247 EncodingVersionMismatch { version_array: String, rlink_version: u32 },
248 RustcVersionMismatch { rustc_version: String },
249 CorruptFile,
250}
251
252pub fn provide(providers: &mut Providers) {
253 crate::back::symbol_export::provide(providers);
254 crate::base::provide(providers);
255 crate::target_features::provide(providers);
256 crate::codegen_attrs::provide(providers);
257 providers.queries.global_backend_features = |_tcx: TyCtxt<'_>, ()| vec![];
258}
259
260pub fn looks_like_rust_object_file(filename: &str) -> bool {
263 let path = Path::new(filename);
264 let ext = path.extension().and_then(|s| s.to_str());
265 if ext != Some(OutputType::Object.extension()) {
266 return false;
268 }
269
270 let ext2 = path.file_stem().and_then(|s| Path::new(s).extension()).and_then(|s| s.to_str());
272
273 ext2 == Some(RUST_CGU_EXT)
275}
276
277const RLINK_VERSION: u32 = 1;
278const RLINK_MAGIC: &[u8] = b"rustlink";
279
280impl CodegenResults {
281 pub fn serialize_rlink(
282 sess: &Session,
283 rlink_file: &Path,
284 codegen_results: &CodegenResults,
285 outputs: &OutputFilenames,
286 ) -> Result<usize, io::Error> {
287 let mut encoder = FileEncoder::new(rlink_file)?;
288 encoder.emit_raw_bytes(RLINK_MAGIC);
289 encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes());
292 encoder.emit_str(sess.cfg_version);
293 Encodable::encode(codegen_results, &mut encoder);
294 Encodable::encode(outputs, &mut encoder);
295 encoder.finish().map_err(|(_path, err)| err)
296 }
297
298 pub fn deserialize_rlink(
299 sess: &Session,
300 data: Vec<u8>,
301 ) -> Result<(Self, OutputFilenames), CodegenErrors> {
302 if !data.starts_with(RLINK_MAGIC) {
305 return Err(CodegenErrors::WrongFileType);
306 }
307 let data = &data[RLINK_MAGIC.len()..];
308 if data.len() < 4 {
309 return Err(CodegenErrors::EmptyVersionNumber);
310 }
311
312 let mut version_array: [u8; 4] = Default::default();
313 version_array.copy_from_slice(&data[..4]);
314 if u32::from_be_bytes(version_array) != RLINK_VERSION {
315 return Err(CodegenErrors::EncodingVersionMismatch {
316 version_array: String::from_utf8_lossy(&version_array).to_string(),
317 rlink_version: RLINK_VERSION,
318 });
319 }
320
321 let Ok(mut decoder) = MemDecoder::new(&data[4..], 0) else {
322 return Err(CodegenErrors::CorruptFile);
323 };
324 let rustc_version = decoder.read_str();
325 if rustc_version != sess.cfg_version {
326 return Err(CodegenErrors::RustcVersionMismatch {
327 rustc_version: rustc_version.to_string(),
328 });
329 }
330
331 let codegen_results = CodegenResults::decode(&mut decoder);
332 let outputs = OutputFilenames::decode(&mut decoder);
333 Ok((codegen_results, outputs))
334 }
335}
336
337#[derive(Copy, Clone, Debug, Encodable, Decodable)]
343pub struct CodegenLintLevels {
344 linker_messages: (Level, LintLevelSource),
345}
346
347impl CodegenLintLevels {
348 pub fn from_tcx(tcx: TyCtxt<'_>) -> Self {
349 Self { linker_messages: tcx.lint_level_at_node(LINKER_MESSAGES, CRATE_HIR_ID) }
350 }
351}