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
use std::slice;
use std::mem;
use std::path::Path;
use std::ffi::CString;
use std::default::Default;
use super::llvm_sys::lto::*;
use super::get_error_msg;
use super::module::{
ObjFile,
Object,
LinkerObject
};
#[derive(Copy,Clone,Debug)]
pub enum PIC {
Static,
Dynamic,
DynamicNoPIC,
Default
}
impl Default for PIC {
#[inline(always)]
fn default() -> PIC {
PIC::Default
}
}
impl Into<lto_codegen_model> for PIC {
#[inline(always)]
fn into(self) -> lto_codegen_model {
match self.clone() {
PIC::Static => lto_codegen_model::LTO_CODEGEN_PIC_MODEL_STATIC,
PIC::Dynamic => lto_codegen_model::LTO_CODEGEN_PIC_MODEL_DYNAMIC,
PIC::DynamicNoPIC => lto_codegen_model::LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC,
PIC::Default => lto_codegen_model::LTO_CODEGEN_PIC_MODEL_DEFAULT
}
}
}
#[allow(dead_code)]
pub struct Linker {
ptr: lto_code_gen_t,
symbols: Vec<CString>,
data: Vec<Vec<u8>>
}
impl Drop for Linker {
fn drop(&mut self) {
unsafe {lto_codegen_dispose(self.ptr)};
}
}
impl Linker {
pub fn new(keep_dwarf: bool, embed_use_list: bool, should_internalize: bool, pic_mode: PIC, keep_symbols: &[&str]) -> Result<Linker,String> {
let linker = unsafe {lto_codegen_create()};
if linker.is_null() {
return Err(get_error_msg());
}
let mut keep = Vec::with_capacity(0);
let debug = if keep_dwarf.clone() {
lto_debug_model::LTO_DEBUG_MODEL_DWARF
} else {
lto_debug_model::LTO_DEBUG_MODEL_NONE
};
let flag = unsafe {lto_codegen_set_debug_model(linker, debug)};
if flag != 0 {
return Err(get_error_msg());
}
let flag = if embed_use_list.clone() { 1 } else { 0 };
unsafe {lto_codegen_set_should_embed_uselists(linker, flag)};
let flag = if should_internalize.clone() { 1 } else { 0 };
unsafe {lto_codegen_set_should_internalize(linker, flag)};
let flag = unsafe {lto_codegen_set_pic_model(linker, pic_mode.clone().into())};
if flag != 0 {
return Err(get_error_msg());
}
for s in keep_symbols {
let symbol = CString::new(s.to_string()).unwrap();
let p = symbol.as_ptr();
unsafe {lto_codegen_add_must_preserve_symbol(linker, p as *const _)};
keep.push(symbol);
}
Ok(Linker {
ptr: linker,
symbols: keep,
data: Vec::with_capacity(0)
})
}
pub fn add_file(&mut self, object_file: ObjFile) {
let ptr = object_file.as_object();
unsafe {lto_codegen_set_module(self.ptr, ptr)};
mem::forget(object_file);
}
pub fn add_buffer(&mut self, object_buffer: Object) {
let ptr = object_buffer.as_object();
let mut vv = Vec::with_capacity(0);
let mut obj = object_buffer;
obj.get_buffer(&mut vv);
self.data.push(vv);
unsafe {lto_codegen_set_module(self.ptr, ptr)};
mem::forget(obj);
}
pub fn link_to_mem(self) -> Result<Object,String> {
let mut len = 0usize;
let ptr = unsafe {lto_codegen_compile_optimized(self.ptr, &mut len)};
if ptr.is_null() {
return Err(get_error_msg());
}
let buffer = unsafe{ slice::from_raw_parts(ptr as *const u8, len)};
Object::from_slice(buffer)
}
pub fn link_to_file<P: AsRef<Path>>(self, path: P) -> Result<(),String> {
let path = CString::new(path.as_ref().to_string_lossy().to_string()).unwrap();
let flag = unsafe {lto_codegen_compile_to_file(self.ptr, path.as_ptr() as *mut _)};
if flag != 0 {
return Err(get_error_msg());
}
Ok(())
}
}