pub fn read_version(dylib_path: &AbsPath) -> Result<String>
Expand description

Check the version of rustc that was used to compile a proc macro crate’s binary file.

A proc macro crate binary’s “.rustc” section has following byte layout:

  • [b’r’,b’u’,b’s’,b’t’,0,0,0,5] is the first 8 bytes
  • ff060000 734e6150 is followed, it’s the snappy format magic bytes, means bytes from here(including this sequence) are compressed in snappy compression format. Version info is inside here, so decompress this. The bytes you get after decompressing the snappy format portion has following layout:
  • [b’r’,b’u’,b’s’,b’t’,0,0,0,5] is the first 8 bytes(again)
  • [crate root bytes] next 8 bytes (4 in old versions) is to store crate root position, according to rustc’s source code comment
  • [length byte] next 1 byte tells us how many bytes we should read next for the version string’s utf8 bytes
  • [version string bytes encoded in utf8] <- GET THIS BOI
  • [some more bytes that we don’t really care but about still there] :-) Check this issue for more about the bytes layout: https://github.com/rust-lang/rust-analyzer/issues/6174