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