wasmer/src/webassembly/relocation.rs

144 lines
4.0 KiB
Rust
Raw Normal View History

2018-10-19 09:31:10 +00:00
//! The relocation package provide two structures: RelocSink, TrapSink.
//! This structures are used by Cranelift when compiling functions to mark
//! any other calls that this function is doing, so we can "patch" the
//! function addrs in runtime with the functions we need.
2018-10-15 00:48:59 +00:00
use cranelift_codegen::binemit;
2018-10-24 00:01:46 +00:00
use cranelift_codegen::ir::{self, ExternalName, LibCall, SourceLoc, TrapCode};
2018-10-15 00:48:59 +00:00
2018-10-15 22:04:05 +00:00
pub use cranelift_codegen::binemit::Reloc;
2018-10-24 09:39:00 +00:00
#[derive(Debug, Clone)]
2018-10-15 00:48:59 +00:00
pub struct Relocation {
/// The relocation code.
pub reloc: binemit::Reloc,
/// The offset where to apply the relocation.
pub offset: binemit::CodeOffset,
/// The addend to add to the relocation value.
pub addend: binemit::Addend,
2018-10-24 09:39:00 +00:00
/// Relocation type.
pub target: RelocationType,
2018-10-15 00:48:59 +00:00
}
/// Specify the type of relocation
2018-10-24 09:39:00 +00:00
#[derive(Debug, Clone)]
2018-10-15 00:48:59 +00:00
pub enum RelocationType {
Normal(u32),
Intrinsic(String),
2018-10-23 23:14:59 +00:00
LibCall(LibCall),
GrowMemory,
CurrentMemory,
2018-10-15 00:48:59 +00:00
}
/// Implementation of a relocation sink that just saves all the information for later
pub struct RelocSink {
/// Relocations recorded for the function.
2018-10-24 09:39:00 +00:00
pub func_relocs: Vec<Relocation>,
2018-10-15 00:48:59 +00:00
}
impl binemit::RelocSink for RelocSink {
fn reloc_ebb(
&mut self,
_offset: binemit::CodeOffset,
_reloc: binemit::Reloc,
_ebb_offset: binemit::CodeOffset,
) {
// This should use the `offsets` field of `ir::Function`.
unimplemented!();
}
fn reloc_external(
&mut self,
offset: binemit::CodeOffset,
reloc: binemit::Reloc,
name: &ExternalName,
addend: binemit::Addend,
) {
match *name {
ExternalName::User {
namespace: 0,
index,
} => {
2018-10-24 09:39:00 +00:00
self.func_relocs.push(Relocation {
reloc,
offset,
addend,
target: RelocationType::Normal(index as _),
});
2018-10-15 01:03:00 +00:00
}
ExternalName::TestCase { length, ascii } => {
2018-10-15 00:48:59 +00:00
let (slice, _) = ascii.split_at(length as usize);
let name = String::from_utf8(slice.to_vec()).unwrap();
let relocation_type = match name.as_str() {
"current_memory" => RelocationType::CurrentMemory,
"grow_memory" => RelocationType::GrowMemory,
_ => RelocationType::Intrinsic(name),
};
2018-10-24 09:39:00 +00:00
self.func_relocs.push(Relocation {
reloc,
offset,
addend,
target: relocation_type,
});
2018-10-15 01:03:00 +00:00
}
2018-10-23 23:14:59 +00:00
ExternalName::LibCall(libcall) => {
let relocation_type = RelocationType::LibCall(libcall);
2018-10-24 09:39:00 +00:00
self.func_relocs.push(Relocation {
reloc,
offset,
addend,
target: relocation_type,
});
2018-10-23 23:14:59 +00:00
}
2018-10-15 00:48:59 +00:00
_ => {
unimplemented!();
}
}
}
fn reloc_jt(
&mut self,
_offset: binemit::CodeOffset,
_reloc: binemit::Reloc,
_jt: ir::JumpTable,
) {
unimplemented!();
}
}
2018-10-24 09:39:00 +00:00
/// Implementation of a relocation sink that just saves all the information for later
2018-10-15 00:48:59 +00:00
impl RelocSink {
pub fn new() -> RelocSink {
RelocSink {
func_relocs: Vec::new(),
}
}
}
pub struct TrapData {
pub offset: usize,
pub code: TrapCode,
}
/// Simple implementation of a TrapSink
/// that saves the info for later.
pub struct TrapSink {
current_func_offset: usize,
trap_datas: Vec<TrapData>,
}
impl TrapSink {
pub fn new(current_func_offset: usize) -> TrapSink {
TrapSink {
current_func_offset,
trap_datas: Vec::new(),
}
}
}
impl binemit::TrapSink for TrapSink {
fn trap(&mut self, offset: u32, _: SourceLoc, code: TrapCode) {
self.trap_datas.push(TrapData {
offset: self.current_func_offset + offset as usize,
code,
});
}
}