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