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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use crate::error::ObjectError;
use object::write::{Object, Relocation, StandardSection, Symbol as ObjSymbol, SymbolSection};
use object::{RelocationEncoding, RelocationKind, SymbolFlags, SymbolKind, SymbolScope};
use wasmer_compiler::{
    Architecture, BinaryFormat, Compilation, CustomSectionProtection, Endianness, RelocationTarget,
    Symbol, SymbolRegistry, Triple,
};

/// Create an object for a given target `Triple`.
///
/// # Usage
///
/// ```rust
/// # use wasmer_compiler::Triple;
/// # use wasmer_object::ObjectError;
/// use wasmer_object::get_object_for_target;
///
/// # fn generate_object_for_target(triple: &Triple) -> Result<(), ObjectError> {
/// let mut object = get_object_for_target(&triple)?;
///
/// # Ok(())
/// # }
/// ```
pub fn get_object_for_target(triple: &Triple) -> Result<Object, ObjectError> {
    let obj_binary_format = match triple.binary_format {
        BinaryFormat::Elf => object::BinaryFormat::Elf,
        BinaryFormat::Macho => object::BinaryFormat::MachO,
        BinaryFormat::Coff => object::BinaryFormat::Coff,
        binary_format => {
            return Err(ObjectError::UnsupportedBinaryFormat(format!(
                "{}",
                binary_format
            )));
        }
    };
    let obj_architecture = match triple.architecture {
        Architecture::X86_64 => object::Architecture::X86_64,
        Architecture::Aarch64(_) => object::Architecture::Aarch64,
        architecture => {
            return Err(ObjectError::UnsupportedArchitecture(format!(
                "{}",
                architecture
            )));
        }
    };
    let obj_endianness = match triple
        .endianness()
        .map_err(|_| ObjectError::UnknownEndianness)?
    {
        Endianness::Little => object::Endianness::Little,
        Endianness::Big => object::Endianness::Big,
    };

    Ok(Object::new(
        obj_binary_format,
        obj_architecture,
        obj_endianness,
    ))
}

/// Write data into an existing object.
///
/// # Usage
///
/// ```rust
/// # use wasmer_compiler::Triple;
/// # use wasmer_object::ObjectError;
/// use wasmer_object::{get_object_for_target, emit_data};
///
/// # fn emit_data_into_object(triple: &Triple) -> Result<(), ObjectError> {
/// let mut object = get_object_for_target(&triple)?;
/// emit_data(&mut object, b"WASMER_METADATA", &b"Hello, World!"[..])?;
///
/// # Ok(())
/// # }
/// ```
pub fn emit_data(obj: &mut Object, name: &[u8], data: &[u8]) -> Result<(), ObjectError> {
    let symbol_id = obj.add_symbol(ObjSymbol {
        name: name.to_vec(),
        value: 0,
        size: 0,
        kind: SymbolKind::Data,
        scope: SymbolScope::Dynamic,
        weak: false,
        section: SymbolSection::Undefined,
        flags: SymbolFlags::None,
    });
    let section_id = obj.section_id(StandardSection::Data);
    obj.add_symbol_data(symbol_id, section_id, &data, 1);

    Ok(())
}

/// Emit the compilation result into an existing object.
///
/// # Usage
///
/// ```rust
/// # use wasmer_compiler::{Compilation, SymbolRegistry, Triple};
/// # use wasmer_object::ObjectError;
/// use wasmer_object::{get_object_for_target, emit_compilation};
///
/// # fn emit_compilation_into_object(
/// #     triple: &Triple,
/// #     compilation: Compilation,
/// #     symbol_registry: impl SymbolRegistry,
/// # ) -> Result<(), ObjectError> {
/// let mut object = get_object_for_target(&triple)?;
/// emit_compilation(&mut object, compilation, &symbol_registry, &triple)?;
/// # Ok(())
/// # }
/// ```
pub fn emit_compilation(
    obj: &mut Object,
    compilation: Compilation,
    symbol_registry: &impl SymbolRegistry,
    triple: &Triple,
) -> Result<(), ObjectError> {
    let function_bodies = compilation.get_function_bodies();
    let function_relocations = compilation.get_relocations();
    let custom_sections = compilation.get_custom_sections();
    let custom_section_relocations = compilation.get_custom_section_relocations();
    let function_call_trampolines = compilation.get_function_call_trampolines();
    let dynamic_function_trampolines = compilation.get_dynamic_function_trampolines();

    // Add sections
    for (section_index, custom_section) in custom_sections.iter() {
        // TODO: We need to rename the sections corresponding to the DWARF information
        // to the proper names (like `.eh_frame`)
        let section_name = symbol_registry.symbol_to_name(Symbol::Section(section_index));
        let (section_kind, standard_section) = match custom_section.protection {
            CustomSectionProtection::ReadExecute => (SymbolKind::Text, StandardSection::Text),
            // TODO: Fix this to be StandardSection::Data
            CustomSectionProtection::Read => (SymbolKind::Data, StandardSection::Text),
        };
        let symbol_id = obj.add_symbol(ObjSymbol {
            name: section_name.into_bytes(),
            value: 0,
            size: 0,
            kind: section_kind,
            scope: SymbolScope::Dynamic,
            weak: false,
            section: SymbolSection::Undefined,
            flags: SymbolFlags::None,
        });
        let section_id = obj.section_id(standard_section);
        obj.add_symbol_data(symbol_id, section_id, custom_section.bytes.as_slice(), 1);
    }

    // Add functions
    for (function_local_index, function) in function_bodies.into_iter() {
        let function_name =
            symbol_registry.symbol_to_name(Symbol::LocalFunction(function_local_index));
        let symbol_id = obj.add_symbol(ObjSymbol {
            name: function_name.into_bytes(),
            value: 0,
            size: 0,
            kind: SymbolKind::Text,
            scope: SymbolScope::Dynamic,
            weak: false,
            section: SymbolSection::Undefined,
            flags: SymbolFlags::None,
        });

        let section_id = obj.section_id(StandardSection::Text);
        obj.add_symbol_data(symbol_id, section_id, &function.body, 1);
    }

    // Add function call trampolines
    for (signature_index, function) in function_call_trampolines.into_iter() {
        let function_name =
            symbol_registry.symbol_to_name(Symbol::FunctionCallTrampoline(signature_index));
        let symbol_id = obj.add_symbol(ObjSymbol {
            name: function_name.into_bytes(),
            value: 0,
            size: 0,
            kind: SymbolKind::Text,
            scope: SymbolScope::Dynamic,
            weak: false,
            section: SymbolSection::Undefined,
            flags: SymbolFlags::None,
        });
        let section_id = obj.section_id(StandardSection::Text);
        obj.add_symbol_data(symbol_id, section_id, &function.body, 1);
    }

    // Add dynamic function trampolines
    for (func_index, function) in dynamic_function_trampolines.into_iter() {
        let function_name =
            symbol_registry.symbol_to_name(Symbol::DynamicFunctionTrampoline(func_index));
        let symbol_id = obj.add_symbol(ObjSymbol {
            name: function_name.into_bytes(),
            value: 0,
            size: 0,
            kind: SymbolKind::Text,
            scope: SymbolScope::Dynamic,
            weak: false,
            section: SymbolSection::Undefined,
            flags: SymbolFlags::None,
        });
        let section_id = obj.section_id(StandardSection::Text);
        obj.add_symbol_data(symbol_id, section_id, &function.body, 1);
    }

    // Add relocations (function and sections)
    let (relocation_size, relocation_kind, relocation_encoding) = match triple.architecture {
        Architecture::X86_64 => (
            32,
            RelocationKind::PltRelative,
            RelocationEncoding::X86Branch,
        ),
        // Object doesn't fully support it yet
        // Architecture::Aarch64(_) => (
        //     32,
        //     RelocationKind::PltRelative,
        //     RelocationEncoding::Generic,
        // ),
        architecture => {
            return Err(ObjectError::UnsupportedArchitecture(
                architecture.to_string(),
            ))
        }
    };
    let mut all_relocations = Vec::new();

    for (function_local_index, relocations) in function_relocations.into_iter() {
        let function_name =
            symbol_registry.symbol_to_name(Symbol::LocalFunction(function_local_index));
        let symbol_id = obj.symbol_id(function_name.as_bytes()).unwrap();
        all_relocations.push((symbol_id, relocations))
    }

    for (section_index, relocations) in custom_section_relocations.into_iter() {
        let section_name = symbol_registry.symbol_to_name(Symbol::Section(section_index));
        let symbol_id = obj.symbol_id(section_name.as_bytes()).unwrap();
        all_relocations.push((symbol_id, relocations))
    }

    for (symbol_id, relocations) in all_relocations.into_iter() {
        let (_symbol_id, section_offset) = obj.symbol_section_and_offset(symbol_id).unwrap();
        let section_id = obj.section_id(StandardSection::Text);

        for r in relocations {
            let relocation_address = section_offset + r.offset as u64;

            match r.reloc_target {
                RelocationTarget::LocalFunc(index) => {
                    let target_name = symbol_registry.symbol_to_name(Symbol::LocalFunction(index));
                    let target_symbol = obj.symbol_id(target_name.as_bytes()).unwrap();
                    obj.add_relocation(
                        section_id,
                        Relocation {
                            offset: relocation_address,
                            size: relocation_size,
                            kind: relocation_kind,
                            encoding: relocation_encoding,
                            symbol: target_symbol,
                            addend: r.addend,
                        },
                    )
                    .map_err(ObjectError::Write)?;
                }
                RelocationTarget::LibCall(libcall) => {
                    let libcall_fn_name = libcall.to_function_name().as_bytes();
                    // We add the symols lazily as we see them
                    let target_symbol = obj.symbol_id(libcall_fn_name).unwrap_or_else(|| {
                        obj.add_symbol(ObjSymbol {
                            name: libcall_fn_name.to_vec(),
                            value: 0,
                            size: 0,
                            kind: SymbolKind::Unknown,
                            scope: SymbolScope::Unknown,
                            weak: false,
                            section: SymbolSection::Undefined,
                            flags: SymbolFlags::None,
                        })
                    });
                    obj.add_relocation(
                        section_id,
                        Relocation {
                            offset: relocation_address,
                            size: relocation_size,
                            kind: relocation_kind,
                            encoding: relocation_encoding,
                            symbol: target_symbol,
                            addend: r.addend,
                        },
                    )
                    .map_err(ObjectError::Write)?;
                }
                RelocationTarget::CustomSection(section_index) => {
                    let target_name =
                        symbol_registry.symbol_to_name(Symbol::Section(section_index));
                    let target_symbol = obj.symbol_id(target_name.as_bytes()).unwrap();
                    obj.add_relocation(
                        section_id,
                        Relocation {
                            offset: relocation_address,
                            size: relocation_size,
                            kind: relocation_kind,
                            encoding: relocation_encoding,
                            symbol: target_symbol,
                            addend: r.addend,
                        },
                    )
                    .map_err(ObjectError::Write)?;
                }
                RelocationTarget::JumpTable(_func_index, _jt) => {
                    // do nothing
                }
            };
        }
    }

    Ok(())
}