aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/proc_macro_api/src/version.rs43
1 files changed, 22 insertions, 21 deletions
diff --git a/crates/proc_macro_api/src/version.rs b/crates/proc_macro_api/src/version.rs
index 80cd183a0..11a7fb59a 100644
--- a/crates/proc_macro_api/src/version.rs
+++ b/crates/proc_macro_api/src/version.rs
@@ -64,8 +64,8 @@ pub(crate) fn read_info(dylib_path: &Path) -> io::Result<RustCInfo> {
64 Ok(RustCInfo { version, channel, commit, date }) 64 Ok(RustCInfo { version, channel, commit, date })
65} 65}
66 66
67// This is used inside read_version() to locate the ".rustc" section 67/// This is used inside read_version() to locate the ".rustc" section
68// from a proc macro crate's binary file. 68/// from a proc macro crate's binary file.
69fn read_section<'a>(dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'a [u8]> { 69fn read_section<'a>(dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'a [u8]> {
70 BinaryFile::parse(dylib_binary) 70 BinaryFile::parse(dylib_binary)
71 .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))? 71 .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
@@ -75,25 +75,26 @@ fn read_section<'a>(dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'
75 .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) 75 .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
76} 76}
77 77
78// Check the version of rustc that was used to compile a proc macro crate's 78/// Check the version of rustc that was used to compile a proc macro crate's
79// binary file. 79///
80// A proc macro crate binary's ".rustc" section has following byte layout: 80/// binary file.
81// * [b'r',b'u',b's',b't',0,0,0,5] is the first 8 bytes 81/// A proc macro crate binary's ".rustc" section has following byte layout:
82// * ff060000 734e6150 is followed, it's the snappy format magic bytes, 82/// * [b'r',b'u',b's',b't',0,0,0,5] is the first 8 bytes
83// means bytes from here(including this sequence) are compressed in 83/// * ff060000 734e6150 is followed, it's the snappy format magic bytes,
84// snappy compression format. Version info is inside here, so decompress 84/// means bytes from here(including this sequence) are compressed in
85// this. 85/// snappy compression format. Version info is inside here, so decompress
86// The bytes you get after decompressing the snappy format portion has 86/// this.
87// following layout: 87/// The bytes you get after decompressing the snappy format portion has
88// * [b'r',b'u',b's',b't',0,0,0,5] is the first 8 bytes(again) 88/// following layout:
89// * [crate root bytes] next 4 bytes is to store crate root position, 89/// * [b'r',b'u',b's',b't',0,0,0,5] is the first 8 bytes(again)
90// according to rustc's source code comment 90/// * [crate root bytes] next 4 bytes is to store crate root position,
91// * [length byte] next 1 byte tells us how many bytes we should read next 91/// according to rustc's source code comment
92// for the version string's utf8 bytes 92/// * [length byte] next 1 byte tells us how many bytes we should read next
93// * [version string bytes encoded in utf8] <- GET THIS BOI 93/// for the version string's utf8 bytes
94// * [some more bytes that we don really care but still there] :-) 94/// * [version string bytes encoded in utf8] <- GET THIS BOI
95// Check this issue for more about the bytes layout: 95/// * [some more bytes that we don really care but still there] :-)
96// https://github.com/rust-analyzer/rust-analyzer/issues/6174 96/// Check this issue for more about the bytes layout:
97/// https://github.com/rust-analyzer/rust-analyzer/issues/6174
97fn read_version(dylib_path: &Path) -> io::Result<String> { 98fn read_version(dylib_path: &Path) -> io::Result<String> {
98 let dylib_file = File::open(dylib_path)?; 99 let dylib_file = File::open(dylib_path)?;
99 let dylib_mmaped = unsafe { Mmap::map(&dylib_file) }?; 100 let dylib_mmaped = unsafe { Mmap::map(&dylib_file) }?;