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.
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.
11 //! Management of the encoding of LLVM bytecode into rlibs
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.
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:
23 //! RLIB LLVM-BYTECODE OBJECT LAYOUT
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
32 //! n+9.. compressed LLVM bitcode
33 //! ? maybe a byte to make this whole thing even length
35 use std::io::{Read, Write};
39 use flate2::Compression;
40 use flate2::read::DeflateDecoder;
41 use flate2::write::DeflateEncoder;
43 // This is the "magic number" expected at the beginning of a LLVM bytecode
45 pub const RLIB_BYTECODE_OBJECT_MAGIC: &[u8] = b"RUST_OBJECT";
47 // The version number this compiler will write to bytecode objects in rlibs
48 pub const RLIB_BYTECODE_OBJECT_VERSION: u8 = 2;
50 pub const RLIB_BYTECODE_EXTENSION: &str = "bc.z";
52 pub fn encode(identifier: &str, bytecode: &[u8]) -> Vec<u8> {
53 let mut encoded = Vec::new();
55 // Start off with the magic string
56 encoded.extend_from_slice(RLIB_BYTECODE_OBJECT_MAGIC);
58 // Next up is the version
59 encoded.extend_from_slice(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]);
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,
69 encoded.extend_from_slice(identifier.as_bytes());
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]);
76 let before = encoded.len();
77 DeflateEncoder::new(&mut encoded, Compression::fast())
80 let after = encoded.len();
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;
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
96 if encoded.len() % 2 == 1 {
103 pub struct DecodedBytecode<'a> {
105 encoded_bytecode: &'a [u8],
108 impl<'a> DecodedBytecode<'a> {
109 pub fn new(data: &'a [u8]) -> Result<DecodedBytecode<'a>, &'static str> {
110 if !data.starts_with(RLIB_BYTECODE_OBJECT_MAGIC) {
111 return Err("magic bytecode prefix not found")
113 let data = &data[RLIB_BYTECODE_OBJECT_MAGIC.len()..];
114 if !data.starts_with(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]) {
115 return Err("wrong version prefix found in bytecode")
117 let data = &data[4..];
119 return Err("bytecode corrupted")
121 let identifier_len = unsafe {
122 u32::from_le(ptr::read_unaligned(data.as_ptr() as *const u32)) as usize
124 let data = &data[4..];
125 if data.len() < identifier_len {
126 return Err("bytecode corrupted")
128 let identifier = match str::from_utf8(&data[..identifier_len]) {
130 Err(_) => return Err("bytecode corrupted")
132 let data = &data[identifier_len..];
134 return Err("bytecode corrupted")
136 let bytecode_len = unsafe {
137 u64::from_le(ptr::read_unaligned(data.as_ptr() as *const u64)) as usize
139 let data = &data[8..];
140 if data.len() < bytecode_len {
141 return Err("bytecode corrupted")
143 let encoded_bytecode = &data[..bytecode_len];
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();
157 pub fn identifier(&self) -> &'a str {