about summary refs log tree commit diff
path: root/compiler/rustc_codegen_ssa/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-04-02 20:58:33 +0000
committerbors <bors@rust-lang.org>2022-04-02 20:58:33 +0000
commit76d770ac21d9521db6a92a48c7b3d5b2cc535941 (patch)
tree9795d3e6cc67d2ae9fdf407c923076f2960ab531 /compiler/rustc_codegen_ssa/src
parent8f96ef4bb56f5d905ed89ed569ef97f50731c977 (diff)
parent0e528f062d51d90368728c163d66297173b07080 (diff)
Auto merge of #95600 - Dylan-DPC:rollup-580y2ra, r=Dylan-DPC
Rollup of 4 pull requests

Successful merges:

 - #95587 (Remove need for associated_type_bounds in std.)
 - #95589 (Include a header in .rlink files)
 - #95593 (diagnostics: add test case for bogus T:Sized suggestion)
 - #95597 (Refer to u8 by absolute path in expansion of thread_local)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_codegen_ssa/src')
-rw-r--r--compiler/rustc_codegen_ssa/src/lib.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs
index 6cf6be79a86..a2d60472ed9 100644
--- a/compiler/rustc_codegen_ssa/src/lib.rs
+++ b/compiler/rustc_codegen_ssa/src/lib.rs
@@ -29,6 +29,7 @@ use rustc_hir::LangItem;
 use rustc_middle::dep_graph::WorkProduct;
 use rustc_middle::middle::dependency_format::Dependencies;
 use rustc_middle::ty::query::{ExternProviders, Providers};
+use rustc_serialize::{opaque, Decodable, Decoder, Encoder};
 use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
 use rustc_session::cstore::{self, CrateSource};
 use rustc_session::utils::NativeLibKind;
@@ -190,3 +191,53 @@ pub fn looks_like_rust_object_file(filename: &str) -> bool {
     // Check if the "inner" extension
     ext2 == Some(RUST_CGU_EXT)
 }
+
+const RLINK_VERSION: u32 = 1;
+const RLINK_MAGIC: &[u8] = b"rustlink";
+
+const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION");
+
+impl CodegenResults {
+    pub fn serialize_rlink(codegen_results: &CodegenResults) -> Vec<u8> {
+        let mut encoder = opaque::Encoder::new(vec![]);
+        encoder.emit_raw_bytes(RLINK_MAGIC).unwrap();
+        // `emit_raw_bytes` is used to make sure that the version representation does not depend on
+        // Encoder's inner representation of `u32`.
+        encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes()).unwrap();
+        encoder.emit_str(RUSTC_VERSION.unwrap()).unwrap();
+
+        let mut encoder = rustc_serialize::opaque::Encoder::new(encoder.into_inner());
+        rustc_serialize::Encodable::encode(codegen_results, &mut encoder).unwrap();
+        encoder.into_inner()
+    }
+
+    pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, String> {
+        // The Decodable machinery is not used here because it panics if the input data is invalid
+        // and because its internal representation may change.
+        if !data.starts_with(RLINK_MAGIC) {
+            return Err("The input does not look like a .rlink file".to_string());
+        }
+        let data = &data[RLINK_MAGIC.len()..];
+        if data.len() < 4 {
+            return Err("The input does not contain version number".to_string());
+        }
+
+        let mut version_array: [u8; 4] = Default::default();
+        version_array.copy_from_slice(&data[..4]);
+        if u32::from_be_bytes(version_array) != RLINK_VERSION {
+            return Err(".rlink file was produced with encoding version {version_array}, but the current version is {RLINK_VERSION}".to_string());
+        }
+
+        let mut decoder = opaque::Decoder::new(&data[4..], 0);
+        let rustc_version = decoder.read_str();
+        let current_version = RUSTC_VERSION.unwrap();
+        if rustc_version != current_version {
+            return Err(format!(
+                ".rlink file was produced by rustc version {rustc_version}, but the current version is {current_version}."
+            ));
+        }
+
+        let codegen_results = CodegenResults::decode(&mut decoder);
+        Ok(codegen_results)
+    }
+}