about summary refs log tree commit diff
path: root/compiler/rustc_codegen_ssa/src
diff options
context:
space:
mode:
authorJakub Beránek <berykubik@gmail.com>2022-04-02 16:50:08 +0200
committerJakub Beránek <berykubik@gmail.com>2022-04-02 16:50:08 +0200
commite0d42266770dcdb3578a2ea7e14ee91967156a2e (patch)
tree8be4cc0839f644789c21bf27a4d2d90f36cb97ba /compiler/rustc_codegen_ssa/src
parent07a461ad52b8485b184397b453b5436c70f9f460 (diff)
downloadrust-e0d42266770dcdb3578a2ea7e14ee91967156a2e.tar.gz
rust-e0d42266770dcdb3578a2ea7e14ee91967156a2e.zip
Include a header in .rlink files to provide nicer error messages when a wrong file is parsed as .rlink
Diffstat (limited to 'compiler/rustc_codegen_ssa/src')
-rw-r--r--compiler/rustc_codegen_ssa/src/lib.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs
index 6cf6be79a86..38d76afc0e6 100644
--- a/compiler/rustc_codegen_ssa/src/lib.rs
+++ b/compiler/rustc_codegen_ssa/src/lib.rs
@@ -34,6 +34,7 @@ use rustc_session::cstore::{self, CrateSource};
 use rustc_session::utils::NativeLibKind;
 use rustc_span::symbol::Symbol;
 use std::path::{Path, PathBuf};
+use rustc_serialize::{Decodable, Decoder, Encoder, opaque};
 
 pub mod back;
 pub mod base;
@@ -190,3 +191,33 @@ pub fn looks_like_rust_object_file(filename: &str) -> bool {
     // Check if the "inner" extension
     ext2 == Some(RUST_CGU_EXT)
 }
+
+const RLINK_MAGIC: &'static [u8; 5] = b"rlink";
+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();
+        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> {
+        if !data.starts_with(RLINK_MAGIC) {
+            return Err("The input does not look like a .rlink file".to_string());
+        }
+        let mut decoder = opaque::Decoder::new(&data[RLINK_MAGIC.len()..], 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)
+    }
+}