wasmer_compiler/artifact_builders/
trampoline.rs

1//! Trampolines for libcalls.
2//!
3//! This is needed because the target of libcall relocations are not reachable
4//! through normal branch instructions.
5
6#![cfg_attr(not(feature = "compiler"), allow(dead_code))]
7
8use wasmer_types::LibCall;
9use wasmer_types::target::{Architecture, Target};
10
11use crate::types::{
12    relocation::{Relocation, RelocationKind, RelocationTarget},
13    section::{CustomSection, CustomSectionProtection, SectionBody},
14};
15
16// SystemV says that both x16 and x17 are available as intra-procedural scratch
17// registers but Apple's ABI restricts us to use x17.
18// LDR x17, [PC, #8]  51 00 00 58
19// BR x17             20 02 1f d6
20// JMPADDR            00 00 00 00 00 00 00 00
21const AARCH64_TRAMPOLINE: [u8; 16] = [
22    0x51, 0x00, 0x00, 0x58, 0x20, 0x02, 0x1f, 0xd6, 0, 0, 0, 0, 0, 0, 0, 0,
23];
24
25// 2 padding bytes are used to preserve alignment.
26// JMP [RIP + 2]   FF 25 02 00 00 00 [00 00]
27// 64-bit ADDR     00 00 00 00 00 00 00 00
28const X86_64_TRAMPOLINE: [u8; 16] = [
29    0xff, 0x25, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30];
31
32// can it be shorter than this?
33// 4 padding bytes are used to preserve alignment.
34// AUIPC t1,0     17 03 00 00
35// LD t1, 16(t1)  03 33 03 01
36// JR t1          67 00 03 00 [00 00 00 00]
37// JMPADDR        00 00 00 00 00 00 00 00
38const RISCV64_TRAMPOLINE: [u8; 24] = [
39    0x17, 0x03, 0x00, 0x00, 0x03, 0x33, 0x03, 0x01, 0x67, 0x00, 0x03, 0x00, 0, 0, 0, 0, 0, 0, 0, 0,
40    0, 0, 0, 0,
41];
42
43// AUIPC t1,0     17 03 00 00
44// LW t1, 12(t1)  03 a3 c2 00
45// JR t1          67 00 03 00
46// JMPADDR        00 00 00 00
47const RISCV32_TRAMPOLINE: [u8; 16] = [
48    0x17, 0x03, 0x00, 0x00, 0x03, 0xa3, 0xc2, 0x00, 0x67, 0x00, 0x03, 0x00, 0, 0, 0, 0,
49];
50
51// PCADDI r12, 0      0c 00 00 18
52// LD.D r12, r12, 16  8c 41 c0 28
53// JR r12             80 01 00 4c [00 00 00 00]
54// JMPADDR            00 00 00 00 00 00 00 00
55const LOONGARCH64_TRAMPOLINE: [u8; 24] = [
56    0x0c, 0x00, 0x00, 0x18, 0x8c, 0x41, 0xc0, 0x28, 0x80, 0x01, 0x00, 0x4c, 0, 0, 0, 0, 0, 0, 0, 0,
57    0, 0, 0, 0,
58];
59
60fn make_trampoline(
61    target: &Target,
62    libcall: LibCall,
63    code: &mut Vec<u8>,
64    relocations: &mut Vec<Relocation>,
65) {
66    match target.triple().architecture {
67        Architecture::Aarch64(_) => {
68            code.extend(AARCH64_TRAMPOLINE);
69            relocations.push(Relocation {
70                kind: RelocationKind::Abs8,
71                reloc_target: RelocationTarget::LibCall(libcall),
72                offset: code.len() as u32 - 8,
73                addend: 0,
74            });
75        }
76        Architecture::X86_64 => {
77            code.extend(X86_64_TRAMPOLINE);
78            relocations.push(Relocation {
79                kind: RelocationKind::Abs8,
80                reloc_target: RelocationTarget::LibCall(libcall),
81                offset: code.len() as u32 - 8,
82                addend: 0,
83            });
84        }
85        Architecture::Riscv64(_) => {
86            code.extend(RISCV64_TRAMPOLINE);
87            relocations.push(Relocation {
88                kind: RelocationKind::Abs8,
89                reloc_target: RelocationTarget::LibCall(libcall),
90                offset: code.len() as u32 - 8,
91                addend: 0,
92            });
93        }
94        Architecture::Riscv32(_) => {
95            code.extend(RISCV32_TRAMPOLINE);
96            relocations.push(Relocation {
97                kind: RelocationKind::Abs4,
98                reloc_target: RelocationTarget::LibCall(libcall),
99                offset: code.len() as u32 - 4,
100                addend: 0,
101            });
102        }
103        Architecture::LoongArch64 => {
104            code.extend(LOONGARCH64_TRAMPOLINE);
105            relocations.push(Relocation {
106                kind: RelocationKind::Abs8,
107                reloc_target: RelocationTarget::LibCall(libcall),
108                offset: code.len() as u32 - 8,
109                addend: 0,
110            });
111        }
112        arch => panic!("Unsupported architecture: {arch}"),
113    };
114}
115
116/// Returns the length of a libcall trampoline.
117pub fn libcall_trampoline_len(target: &Target) -> usize {
118    match target.triple().architecture {
119        Architecture::Aarch64(_) => AARCH64_TRAMPOLINE.len(),
120        Architecture::X86_64 => X86_64_TRAMPOLINE.len(),
121        Architecture::Riscv64(_) => RISCV64_TRAMPOLINE.len(),
122        Architecture::Riscv32(_) => RISCV32_TRAMPOLINE.len(),
123        Architecture::LoongArch64 => LOONGARCH64_TRAMPOLINE.len(),
124        arch => panic!("Unsupported architecture: {arch}"),
125    }
126}
127
128/// Creates a custom section containing the libcall trampolines.
129pub fn make_libcall_trampolines(target: &Target) -> CustomSection {
130    let mut code = vec![];
131    let mut relocations = vec![];
132    for libcall in enum_iterator::all::<LibCall>() {
133        make_trampoline(target, libcall, &mut code, &mut relocations);
134    }
135    CustomSection {
136        protection: CustomSectionProtection::ReadExecute,
137        alignment: None,
138        bytes: SectionBody::new_with_vec(code),
139        relocations,
140    }
141}
142
143/// Returns the address of a trampoline in the libcall trampolines section.
144pub fn get_libcall_trampoline(
145    libcall: LibCall,
146    libcall_trampolines: usize,
147    libcall_trampoline_len: usize,
148) -> usize {
149    libcall_trampolines + libcall as usize * libcall_trampoline_len
150}