wasmer_engine_universal_artifact/
trampoline.rs1use enum_iterator::IntoEnumIterator;
7use wasmer_compiler::{
8 Architecture, CustomSection, CustomSectionProtection, Relocation, RelocationKind,
9 RelocationTarget, SectionBody, Target,
10};
11use wasmer_types::LibCall;
12
13const AARCH64_TRAMPOLINE: [u8; 16] = [
19 0x51, 0x00, 0x00, 0x58, 0x20, 0x02, 0x1f, 0xd6, 0, 0, 0, 0, 0, 0, 0, 0,
20];
21
22const X86_64_TRAMPOLINE: [u8; 16] = [
26 0xff, 0x25, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27];
28
29fn make_trampoline(
30 target: &Target,
31 libcall: LibCall,
32 code: &mut Vec<u8>,
33 relocations: &mut Vec<Relocation>,
34) {
35 match target.triple().architecture {
36 Architecture::Aarch64(_) => {
37 code.extend(&AARCH64_TRAMPOLINE);
38 relocations.push(Relocation {
39 kind: RelocationKind::Abs8,
40 reloc_target: RelocationTarget::LibCall(libcall),
41 offset: code.len() as u32 - 8,
42 addend: 0,
43 });
44 }
45 Architecture::X86_64 => {
46 code.extend(&X86_64_TRAMPOLINE);
47 relocations.push(Relocation {
48 kind: RelocationKind::Abs8,
49 reloc_target: RelocationTarget::LibCall(libcall),
50 offset: code.len() as u32 - 8,
51 addend: 0,
52 });
53 }
54 arch => panic!("Unsupported architecture: {}", arch),
55 };
56}
57
58pub fn libcall_trampoline_len(target: &Target) -> usize {
60 match target.triple().architecture {
61 Architecture::Aarch64(_) => AARCH64_TRAMPOLINE.len(),
62 Architecture::X86_64 => X86_64_TRAMPOLINE.len(),
63 arch => panic!("Unsupported architecture: {}", arch),
64 }
65}
66
67pub fn make_libcall_trampolines(target: &Target) -> CustomSection {
69 let mut code = vec![];
70 let mut relocations = vec![];
71 for libcall in LibCall::into_enum_iter() {
72 make_trampoline(target, libcall, &mut code, &mut relocations);
73 }
74 CustomSection {
75 protection: CustomSectionProtection::ReadExecute,
76 bytes: SectionBody::new_with_vec(code),
77 relocations,
78 }
79}
80
81pub fn get_libcall_trampoline(
83 libcall: LibCall,
84 libcall_trampolines: usize,
85 libcall_trampoline_len: usize,
86) -> usize {
87 libcall_trampolines + libcall as usize * libcall_trampoline_len
88}