]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/back/bytecode.rs
Auto merge of #56534 - xfix:copied, r=@SimonSapin
[rust.git] / src / librustc_codegen_llvm / back / bytecode.rs
1 //! Management of the encoding of LLVM bytecode into rlibs
2 //!
3 //! This module contains the management of encoding LLVM bytecode into rlibs,
4 //! primarily for the usage in LTO situations. Currently the compiler will
5 //! unconditionally encode LLVM-IR into rlibs regardless of what's happening
6 //! elsewhere, so we currently compress the bytecode via deflate to avoid taking
7 //! up too much space on disk.
8 //!
9 //! After compressing the bytecode we then have the rest of the format to
10 //! basically deal with various bugs in various archive implementations. The
11 //! format currently is:
12 //!
13 //!     RLIB LLVM-BYTECODE OBJECT LAYOUT
14 //!     Version 2
15 //!     Bytes    Data
16 //!     0..10    "RUST_OBJECT" encoded in ASCII
17 //!     11..14   format version as little-endian u32
18 //!     15..19   the length of the module identifier string
19 //!     20..n    the module identifier string
20 //!     n..n+8   size in bytes of deflate compressed LLVM bitcode as
21 //!              little-endian u64
22 //!     n+9..    compressed LLVM bitcode
23 //!     ?        maybe a byte to make this whole thing even length
24
25 use std::io::{Read, Write};
26 use std::ptr;
27 use std::str;
28
29 use flate2::Compression;
30 use flate2::read::DeflateDecoder;
31 use flate2::write::DeflateEncoder;
32
33 // This is the "magic number" expected at the beginning of a LLVM bytecode
34 // object in an rlib.
35 pub const RLIB_BYTECODE_OBJECT_MAGIC: &[u8] = b"RUST_OBJECT";
36
37 // The version number this compiler will write to bytecode objects in rlibs
38 pub const RLIB_BYTECODE_OBJECT_VERSION: u8 = 2;
39
40 pub const RLIB_BYTECODE_EXTENSION: &str = "bc.z";
41
42 pub fn encode(identifier: &str, bytecode: &[u8]) -> Vec<u8> {
43     let mut encoded = Vec::new();
44
45     // Start off with the magic string
46     encoded.extend_from_slice(RLIB_BYTECODE_OBJECT_MAGIC);
47
48     // Next up is the version
49     encoded.extend_from_slice(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]);
50
51     // Next is the LLVM module identifier length + contents
52     let identifier_len = identifier.len();
53     encoded.extend_from_slice(&[
54         (identifier_len >>  0) as u8,
55         (identifier_len >>  8) as u8,
56         (identifier_len >> 16) as u8,
57         (identifier_len >> 24) as u8,
58     ]);
59     encoded.extend_from_slice(identifier.as_bytes());
60
61     // Next is the LLVM module deflate compressed, prefixed with its length. We
62     // don't know its length yet, so fill in 0s
63     let deflated_size_pos = encoded.len();
64     encoded.extend_from_slice(&[0, 0, 0, 0, 0, 0, 0, 0]);
65
66     let before = encoded.len();
67     DeflateEncoder::new(&mut encoded, Compression::fast())
68         .write_all(bytecode)
69         .unwrap();
70     let after = encoded.len();
71
72     // Fill in the length we reserved space for before
73     let bytecode_len = (after - before) as u64;
74     encoded[deflated_size_pos + 0] = (bytecode_len >>  0) as u8;
75     encoded[deflated_size_pos + 1] = (bytecode_len >>  8) as u8;
76     encoded[deflated_size_pos + 2] = (bytecode_len >> 16) as u8;
77     encoded[deflated_size_pos + 3] = (bytecode_len >> 24) as u8;
78     encoded[deflated_size_pos + 4] = (bytecode_len >> 32) as u8;
79     encoded[deflated_size_pos + 5] = (bytecode_len >> 40) as u8;
80     encoded[deflated_size_pos + 6] = (bytecode_len >> 48) as u8;
81     encoded[deflated_size_pos + 7] = (bytecode_len >> 56) as u8;
82
83     // If the number of bytes written to the object so far is odd, add a
84     // padding byte to make it even. This works around a crash bug in LLDB
85     // (see issue #15950)
86     if encoded.len() % 2 == 1 {
87         encoded.push(0);
88     }
89
90     return encoded
91 }
92
93 pub struct DecodedBytecode<'a> {
94     identifier: &'a str,
95     encoded_bytecode: &'a [u8],
96 }
97
98 impl<'a> DecodedBytecode<'a> {
99     pub fn new(data: &'a [u8]) -> Result<DecodedBytecode<'a>, &'static str> {
100         if !data.starts_with(RLIB_BYTECODE_OBJECT_MAGIC) {
101             return Err("magic bytecode prefix not found")
102         }
103         let data = &data[RLIB_BYTECODE_OBJECT_MAGIC.len()..];
104         if !data.starts_with(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]) {
105             return Err("wrong version prefix found in bytecode")
106         }
107         let data = &data[4..];
108         if data.len() < 4 {
109             return Err("bytecode corrupted")
110         }
111         let identifier_len = unsafe {
112             u32::from_le(ptr::read_unaligned(data.as_ptr() as *const u32)) as usize
113         };
114         let data = &data[4..];
115         if data.len() < identifier_len {
116             return Err("bytecode corrupted")
117         }
118         let identifier = match str::from_utf8(&data[..identifier_len]) {
119             Ok(s) => s,
120             Err(_) => return Err("bytecode corrupted")
121         };
122         let data = &data[identifier_len..];
123         if data.len() < 8 {
124             return Err("bytecode corrupted")
125         }
126         let bytecode_len = unsafe {
127             u64::from_le(ptr::read_unaligned(data.as_ptr() as *const u64)) as usize
128         };
129         let data = &data[8..];
130         if data.len() < bytecode_len {
131             return Err("bytecode corrupted")
132         }
133         let encoded_bytecode = &data[..bytecode_len];
134
135         Ok(DecodedBytecode {
136             identifier,
137             encoded_bytecode,
138         })
139     }
140
141     pub fn bytecode(&self) -> Vec<u8> {
142         let mut data = Vec::new();
143         DeflateDecoder::new(self.encoded_bytecode).read_to_end(&mut data).unwrap();
144         return data
145     }
146
147     pub fn identifier(&self) -> &'a str {
148         self.identifier
149     }
150 }