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()
}
}
pub trait LinkerObject {
fn as_object(&self) -> lto_module_t;
fn get_num_symbols(&self) -> u32 {
unsafe {lto_module_get_num_symbols(self.as_object())}
}
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))
}
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))
}
}
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 {
#[inline(always)]
fn as_object(&self) -> lto_module_t { self.ptr }
}
impl ObjFile {
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)
}
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))
}
}
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)
}
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)
}
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 })
}
#[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
}
#[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
}
}
#[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 {
#[inline(always)]
fn as_object(&self) -> lto_module_t { self.ptr }
}
impl Object {
#[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
})
}
}
pub fn from_slice(obj: &[u8]) -> Result<Object,String> {
Object::from_vec(obj.to_vec())
}
pub fn get_buffer(&mut self, v: &mut Vec<u8>) {
mem::swap(&mut self.buffer, v);
}
}