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