]> git.proxmox.com Git - rustc.git/blame - src/librustc_codegen_llvm/back/bytecode.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / librustc_codegen_llvm / back / bytecode.rs
CommitLineData
ea8adc8c
XL
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
25use std::io::{Read, Write};
26use std::ptr;
27use std::str;
28
ea8adc8c
XL
29use flate2::read::DeflateDecoder;
30use flate2::write::DeflateEncoder;
dfeec247 31use flate2::Compression;
ea8adc8c
XL
32
33// This is the "magic number" expected at the beginning of a LLVM bytecode
34// object in an rlib.
a1dfa0c6 35pub const RLIB_BYTECODE_OBJECT_MAGIC: &[u8] = b"RUST_OBJECT";
ea8adc8c
XL
36
37// The version number this compiler will write to bytecode objects in rlibs
38pub const RLIB_BYTECODE_OBJECT_VERSION: u8 = 2;
39
ea8adc8c
XL
40pub fn encode(identifier: &str, bytecode: &[u8]) -> Vec<u8> {
41 let mut encoded = Vec::new();
42
43 // Start off with the magic string
44 encoded.extend_from_slice(RLIB_BYTECODE_OBJECT_MAGIC);
45
46 // Next up is the version
47 encoded.extend_from_slice(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]);
48
49 // Next is the LLVM module identifier length + contents
50 let identifier_len = identifier.len();
51 encoded.extend_from_slice(&[
dfeec247
XL
52 (identifier_len >> 0) as u8,
53 (identifier_len >> 8) as u8,
ea8adc8c
XL
54 (identifier_len >> 16) as u8,
55 (identifier_len >> 24) as u8,
56 ]);
57 encoded.extend_from_slice(identifier.as_bytes());
58
59 // Next is the LLVM module deflate compressed, prefixed with its length. We
60 // don't know its length yet, so fill in 0s
61 let deflated_size_pos = encoded.len();
62 encoded.extend_from_slice(&[0, 0, 0, 0, 0, 0, 0, 0]);
63
64 let before = encoded.len();
dfeec247 65 DeflateEncoder::new(&mut encoded, Compression::fast()).write_all(bytecode).unwrap();
ea8adc8c
XL
66 let after = encoded.len();
67
68 // Fill in the length we reserved space for before
69 let bytecode_len = (after - before) as u64;
dfeec247
XL
70 encoded[deflated_size_pos + 0] = (bytecode_len >> 0) as u8;
71 encoded[deflated_size_pos + 1] = (bytecode_len >> 8) as u8;
ea8adc8c
XL
72 encoded[deflated_size_pos + 2] = (bytecode_len >> 16) as u8;
73 encoded[deflated_size_pos + 3] = (bytecode_len >> 24) as u8;
74 encoded[deflated_size_pos + 4] = (bytecode_len >> 32) as u8;
75 encoded[deflated_size_pos + 5] = (bytecode_len >> 40) as u8;
76 encoded[deflated_size_pos + 6] = (bytecode_len >> 48) as u8;
77 encoded[deflated_size_pos + 7] = (bytecode_len >> 56) as u8;
78
79 // If the number of bytes written to the object so far is odd, add a
80 // padding byte to make it even. This works around a crash bug in LLDB
81 // (see issue #15950)
82 if encoded.len() % 2 == 1 {
83 encoded.push(0);
84 }
85
ba9703b0 86 encoded
ea8adc8c
XL
87}
88
89pub struct DecodedBytecode<'a> {
90 identifier: &'a str,
91 encoded_bytecode: &'a [u8],
92}
93
94impl<'a> DecodedBytecode<'a> {
a1dfa0c6 95 pub fn new(data: &'a [u8]) -> Result<DecodedBytecode<'a>, &'static str> {
ea8adc8c 96 if !data.starts_with(RLIB_BYTECODE_OBJECT_MAGIC) {
dfeec247 97 return Err("magic bytecode prefix not found");
ea8adc8c
XL
98 }
99 let data = &data[RLIB_BYTECODE_OBJECT_MAGIC.len()..];
100 if !data.starts_with(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]) {
dfeec247 101 return Err("wrong version prefix found in bytecode");
ea8adc8c
XL
102 }
103 let data = &data[4..];
104 if data.len() < 4 {
dfeec247 105 return Err("bytecode corrupted");
ea8adc8c 106 }
dfeec247
XL
107 let identifier_len =
108 unsafe { u32::from_le(ptr::read_unaligned(data.as_ptr() as *const u32)) as usize };
ea8adc8c
XL
109 let data = &data[4..];
110 if data.len() < identifier_len {
dfeec247 111 return Err("bytecode corrupted");
ea8adc8c
XL
112 }
113 let identifier = match str::from_utf8(&data[..identifier_len]) {
114 Ok(s) => s,
dfeec247 115 Err(_) => return Err("bytecode corrupted"),
ea8adc8c
XL
116 };
117 let data = &data[identifier_len..];
118 if data.len() < 8 {
dfeec247 119 return Err("bytecode corrupted");
ea8adc8c 120 }
dfeec247
XL
121 let bytecode_len =
122 unsafe { u64::from_le(ptr::read_unaligned(data.as_ptr() as *const u64)) as usize };
ea8adc8c
XL
123 let data = &data[8..];
124 if data.len() < bytecode_len {
dfeec247 125 return Err("bytecode corrupted");
ea8adc8c
XL
126 }
127 let encoded_bytecode = &data[..bytecode_len];
128
dfeec247 129 Ok(DecodedBytecode { identifier, encoded_bytecode })
ea8adc8c
XL
130 }
131
132 pub fn bytecode(&self) -> Vec<u8> {
133 let mut data = Vec::new();
134 DeflateDecoder::new(self.encoded_bytecode).read_to_end(&mut data).unwrap();
ba9703b0 135 data
ea8adc8c
XL
136 }
137
138 pub fn identifier(&self) -> &'a str {
139 self.identifier
140 }
141}