1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use std::mem;
use std::path::Path;
use std::ffi::{
    CString,
    CStr
};

use super::llvm_sys::lto::*;
use super::get_error_msg;
#[inline(always)]
fn c_str(x: *const i8) -> String {
    if x.is_null() {
        return String::with_capacity(0);
    }
    unsafe {
        CStr::from_ptr(x as *const _)
            .to_string_lossy()
            .trim()
            .to_string()
    }
}

/// Unifying properties that all linker objects posses.
///
/// Object files are known by the type `ObjFile`. This lets the developer inspect them
/// easierly
///
/// ```rust,no_run
/// use llvm_link::{ObjFile,LinkerObject};
///
/// let obj = match ObjFile::new("my/file.o") {
///     Ok(x) => x,
///     Err(e) => panic!("Could not load {}",e)
/// };
/// match obj.get_target_triple() {
///     Ok(x) => println!("m/file.o's target triple is {}",x),
///     Err(e) => panic!("Could not load triple {}",e)
/// };
/// ```
pub trait LinkerObject {

    fn as_object(&self) -> lto_module_t;
    
    /// Get the number of symbols in an object file
    fn get_num_symbols(&self) -> u32 {
        unsafe {lto_module_get_num_symbols(self.as_object())}
    }
    
    /// Get the name of a symbol at a certain index
    fn get_symbol_name(&self, index: u32) -> Result<String,String> {
        let ptr = unsafe {lto_module_get_symbol_name(self.as_object(), index)};
        if ptr.is_null() {
            return Err(get_error_msg());
        }
        Ok(c_str(ptr))
    }
    
    /// Get the target triple an object file was compiled for
    fn get_target_triple(&self) -> Result<String,String> {
        let ptr = unsafe {lto_module_get_target_triple(self.as_object())};
        if ptr.is_null() {
            return Err(get_error_msg());
        }
        Ok(c_str(ptr))
    }
}

/// Object Files loaded from the File System into LLVM directly
///
/// These files are not part of Rust's memory. They remain within
/// the LLVM. The `ObjectFile` type is really only 1 ptr wide
pub struct ObjFile {
    ptr: lto_module_t
}
impl Drop for ObjFile {
    fn drop(&mut self) { unsafe {lto_module_dispose(self.ptr)}; }
}
impl LinkerObject for ObjFile {

    /// Exposes internal C-binding poitner
    #[inline(always)]
    fn as_object(&self) -> lto_module_t { self.ptr }
}
impl ObjFile {

    /// Load an Object File from the file system
    pub fn new<P: AsRef<Path>>(path: P) -> Result<ObjFile,String> {
        let path = CString::new(path.as_ref().to_string_lossy().to_string()).unwrap();
        ObjFile::from_cstr(&path) 
    }

    /// Load an Object File asserting its target triple
    pub fn assert_target_triple<P: AsRef<Path>, S: AsRef<str>>(path: P, target_triple_prefix: S) -> Result<ObjFile,String> {
        let path = CString::new(path.as_ref().to_string_lossy().to_string()).unwrap();
        let tt = CString::new(target_triple_prefix.as_ref()).unwrap(); 
        let flag = ObjFile::is_obj_file_for_target_ffi(&path,&tt);
        if flag {
            ObjFile::from_cstr(&path)
        } else {
            Err(format!("Input file {:?} isn't compatible with triple {:?}",path,tt))
        }
    }
    
    /// Check if path leads to an object file
    pub fn is_object_file<P: AsRef<Path>>(path: P) -> bool {
        let path = CString::new(path.as_ref().to_string_lossy().to_string()).unwrap();
        ObjFile::is_object_file_ffi(&path)   
    }
    
    /// Check if path is an object file for a specific target triple
    pub fn is_object_file_for_target<P: AsRef<Path>, S: AsRef<str>>(path: P, target_triple_prefix: S) -> bool {
        let path = CString::new(path.as_ref().to_string_lossy().to_string()).unwrap();
        let tt = CString::new(target_triple_prefix.as_ref()).unwrap(); 
        ObjFile::is_obj_file_for_target_ffi(&path,&tt)
    }

    /// Functionally this is an identical operation to `ObjFile::new`
    ///
    /// The only difference is the internal path is not re-allocated
    /// as `CStr` implies there is _always_ a null terminator. While
    /// `AsRef<path>` is a very nice Rusty way to handle path variables.
    pub fn from_cstr(path: &CStr) -> Result<ObjFile, String> {
        let p = path.as_ptr();
        let ptr = unsafe {lto_module_create(p as *const _)};
        if ptr.is_null() {
            return Err(get_error_msg());
        }
        Ok(ObjFile{ ptr: ptr })
    }

    /// Functionally this is identical to the operation `ObjFile::is_object_file`
    ///
    /// Just the internal path is not re-allocated
    #[inline]
    pub fn is_object_file_ffi(path: &CStr) -> bool {
        let c = path.as_ptr();
        let flag = unsafe {lto_module_is_object_file(c as *const _)};
        flag == 1
    }

    /// Functionally this is identical to the operation `ObjFile::is_object_file_for_target`
    ///
    /// Just the internal path is not re-allocated
    #[inline]
    pub fn is_obj_file_for_target_ffi(path: &CStr, tt: &CStr) -> bool {
        let p = path.as_ptr();
        let t = tt.as_ptr();
        let flag = unsafe {lto_module_is_object_file_for_target(p as *const _, t as *const _)};
        flag == 1
    }
}


/// Link Time Optimization Object
///
/// Object that is fully loaded into memory. This contains its own buffer
/// as the LLVM is quickly free its memory after generating an Object File.
#[allow(dead_code)]
pub struct Object {
    ptr: lto_module_t,
    buffer: Vec<u8>
}
impl Drop for Object {
    fn drop(&mut self) { unsafe {lto_module_dispose(self.ptr)}; }
}
impl LinkerObject for Object {

    /// Exposes internal C-binding poitner
    #[inline(always)]
    fn as_object(&self) -> lto_module_t { self.ptr }
}
impl Object {

    /// Builds a new object file.
    #[inline]
    pub fn from_vec(obj: Vec<u8>) -> Result<Object,String> {
        let buffer = obj;
        let ptr = unsafe {
            let ptr = buffer.as_ptr();
            let len = buffer.len();
            lto_module_create_from_memory(ptr as *const _, len)
        };
        if ptr.is_null() {
            return Err(get_error_msg());
        } 
        
        else {
            Ok(Object {
                ptr: ptr,
                buffer: buffer
            })
        }
    }

    /// Builds a new object file
    ///
    /// But clones the underlying buffer to ensure ownership
    pub fn from_slice(obj: &[u8]) -> Result<Object,String> {
        Object::from_vec(obj.to_vec())  
    }

    /// Internal method to steal inner buffer.
    ///
    /// Swaps `v` with own internal buffer. Be warned **THE LLVM HAS POINTERS TO THIS**
    /// if `v` is free'd before `self` this can cause memory errors
    pub fn get_buffer(&mut self, v: &mut Vec<u8>) {
        mem::swap(&mut self.buffer, v);
    }
}