wasmer-compiler 7.3.0

Base compiler abstraction for Wasmer WebAssembly runtime
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Helpers shared by the object-based compiler backends (Cranelift and
//! Singlepass) for emitting per-function relocatable ELF objects and linking
//! them into the final module image.

use crate::compiler::{CompiledObjects, emit_metadata_and_link};
use crate::dwarf::{EhRelocation, EhTarget};
use crate::misc::{CompiledFunctionExt, CompiledKind};
use crate::object::get_object_for_target;
use crate::types::function::{Compilation, FunctionBody};
use crate::types::relocation::{Relocation, RelocationKind, RelocationTarget};
use crate::types::section::CustomSection;
use object::{
    RelocationEncoding, RelocationFlags, RelocationKind as ObjectRelocationKind, SectionKind,
    SymbolFlags, SymbolKind, SymbolScope, elf,
    write::{
        Object, Relocation as ObjectRelocation, SectionId, StandardSection, StandardSegment,
        Symbol, SymbolId, SymbolSection,
    },
};
use std::path::PathBuf;
use wasmer_types::{
    CompileError, LibCall, LocalFunctionIndex, TrapInformation, entity::PrimaryMap, target::Target,
};
use wasmer_types::{FunctionIndex, FunctionType};

/// The result of compiling a single unit (function or trampoline): either an
/// in-memory body for the classic artifact format, or a relocatable object
/// buffer for the ELF artifact format (together with the maximum stack usage,
/// when known).
pub enum CompileOutput<T> {
    /// The compiled body, kept in memory.
    InMemory(T),
    /// Serialized relocatable object and the unit's maximum stack usage, when known.
    Object(Vec<u8>, Option<usize>),
}

impl<T: crate::compiler::CompiledFunction> crate::compiler::CompiledFunction for CompileOutput<T> {}

/// Extract the object buffers from ELF-mode compile outputs.
pub fn compile_output_objects<T>(outputs: Vec<CompileOutput<T>>) -> Vec<Vec<u8>> {
    outputs
        .into_iter()
        .map(|output| match output {
            CompileOutput::Object(object, _) => object,
            CompileOutput::InMemory(_) => unreachable!(),
        })
        .collect()
}

/// Extract the in-memory bodies from classic-mode compile outputs.
pub fn compile_output_in_memory<T>(outputs: Vec<CompileOutput<T>>) -> Vec<T> {
    outputs
        .into_iter()
        .map(|output| match output {
            CompileOutput::InMemory(body) => body,
            CompileOutput::Object(..) => unreachable!(),
        })
        .collect()
}

/// Declare an undefined text symbol resolved when the objects are linked.
pub fn add_undefined_symbol(object: &mut Object<'static>, name: String) -> SymbolId {
    object.add_symbol(Symbol {
        name: name.into_bytes(),
        value: 0,
        size: 0,
        kind: SymbolKind::Text,
        scope: SymbolScope::Linkage,
        weak: false,
        section: SymbolSection::Undefined,
        flags: SymbolFlags::None,
    })
}

/// Declare an undefined dynamic symbol for a libcall, resolved by the runtime
/// loader through a dynamic relocation.
pub fn add_libcall_symbol(object: &mut Object<'static>, libcall: LibCall) -> SymbolId {
    object.add_symbol(Symbol {
        name: libcall.to_function_name().to_string().into_bytes(),
        value: 0,
        size: 0,
        kind: SymbolKind::Unknown,
        scope: SymbolScope::Dynamic,
        weak: false,
        section: SymbolSection::Undefined,
        flags: SymbolFlags::None,
    })
}

/// Map a Wasmer relocation kind onto the corresponding object-file relocation
/// flags.
pub fn relocation_kind_to_flags(kind: RelocationKind) -> Result<RelocationFlags, CompileError> {
    use ObjectRelocationKind as K;
    Ok(match kind {
        RelocationKind::Abs4 => RelocationFlags::Generic {
            kind: K::Absolute,
            encoding: RelocationEncoding::Generic,
            size: 32,
        },
        RelocationKind::Abs8 => RelocationFlags::Generic {
            kind: K::Absolute,
            encoding: RelocationEncoding::Generic,
            size: 64,
        },
        RelocationKind::PCRel4 => RelocationFlags::Generic {
            kind: K::Relative,
            encoding: RelocationEncoding::Generic,
            size: 32,
        },
        RelocationKind::X86CallPCRel4 => RelocationFlags::Generic {
            kind: K::Relative,
            encoding: RelocationEncoding::X86Branch,
            size: 32,
        },
        RelocationKind::X86CallPLTRel4 => RelocationFlags::Generic {
            kind: K::PltRelative,
            encoding: RelocationEncoding::X86Branch,
            size: 32,
        },
        RelocationKind::X86GOTPCRel4 => RelocationFlags::Generic {
            kind: K::GotRelative,
            encoding: RelocationEncoding::Generic,
            size: 32,
        },
        RelocationKind::Arm64Call => RelocationFlags::Elf {
            r_type: elf::R_AARCH64_CALL26,
        },
        // For RISC-V relocations, please refer to:
        // https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/2484f950a551c653f1823f1bd11926bf5a57fae3/riscv-elf.adoc#relocations
        RelocationKind::RiscvPCRelHi20 => RelocationFlags::Elf {
            r_type: elf::R_RISCV_PCREL_HI20,
        },
        RelocationKind::RiscvPCRelLo12I => RelocationFlags::Elf {
            r_type: elf::R_RISCV_PCREL_LO12_I,
        },
        RelocationKind::RiscvCall => RelocationFlags::Elf {
            r_type: elf::R_RISCV_CALL_PLT,
        },
        kind => {
            return Err(CompileError::Codegen(format!(
                "unsupported ELF relocation kind: {kind:?}"
            )));
        }
    })
}

/// Apply the compiled code's relocations to `section`, declaring the referenced symbols.
pub fn add_relocations(
    object: &mut Object<'static>,
    section: SectionId,
    relocations: &[Relocation],
    local_symbol: Option<(LocalFunctionIndex, SymbolId)>,
) -> Result<(), CompileError> {
    for relocation in relocations {
        let symbol = match relocation.reloc_target {
            RelocationTarget::LocalFunc(index) => local_symbol
                .filter(|(local_index, _)| *local_index == index)
                .map_or_else(
                    || {
                        add_undefined_symbol(
                            object,
                            CompiledKind::Local(index, String::new()).linkage_name(),
                        )
                    },
                    |(_, symbol)| symbol,
                ),
            RelocationTarget::CustomSection(index) => add_undefined_symbol(
                object,
                CompiledKind::ImportFunctionTrampoline(
                    FunctionIndex::from_u32(index.as_u32()),
                    FunctionType::default(),
                )
                .linkage_name(),
            ),
            RelocationTarget::LibCall(libcall) => add_libcall_symbol(object, libcall),
            RelocationTarget::DynamicTrampoline(index) => add_undefined_symbol(
                object,
                CompiledKind::DynamicFunctionTrampoline(index, FunctionType::default())
                    .linkage_name(),
            ),
        };
        let flags = relocation_kind_to_flags(relocation.kind)?;
        object
            .add_relocation(
                section,
                ObjectRelocation {
                    offset: relocation.offset as u64,
                    flags,
                    symbol,
                    addend: relocation.addend,
                },
            )
            .map_err(|e| CompileError::Codegen(format!("failed to add ELF relocation: {e}")))?;
    }
    Ok(())
}

/// Emit the per-function trap table into a `.w.traps` section, under a weak
/// data symbol so the metadata object can reference it per function.
pub fn emit_trap_section(
    object: &mut Object<'static>,
    kind: &CompiledKind,
    traps: &[TrapInformation],
) {
    let mut trap_data = Vec::with_capacity(traps.len() * 8 + size_of::<u32>());
    trap_data.extend_from_slice(&(traps.len() as u32).to_le_bytes());
    for trap in traps {
        trap_data.extend_from_slice(&trap.code_offset.to_le_bytes());
        trap_data.extend_from_slice(&(trap.trap_code as u32).to_le_bytes());
    }
    let traps_section = object.add_section(
        object.segment_name(StandardSegment::Data).to_vec(),
        crate::WASMER_TRAPS_SECTION_NAME.to_vec(),
        SectionKind::Other,
    );
    let traps_symbol = object.add_symbol(Symbol {
        name: kind.traps_name().into_bytes(),
        value: 0,
        size: trap_data.len() as u64,
        kind: SymbolKind::Data,
        scope: SymbolScope::Linkage,
        weak: true,
        section: SymbolSection::Section(traps_section),
        flags: SymbolFlags::None,
    });
    object.add_symbol_data(traps_symbol, traps_section, &trap_data, 4);
}

/// Emit a serialized `.eh_frame` blob into its own section, resolving the
/// recorded relocations against the function's text symbol, the exception
/// personality routine and the function's LSDA section.
pub fn emit_eh_frame_section(
    object: &mut Object<'static>,
    eh_frame_bytes: &[u8],
    relocations: &[EhRelocation],
    function_symbol: SymbolId,
    lsda_section_symbol: Option<SymbolId>,
) -> Result<(), CompileError> {
    let section = object.add_section(
        object.segment_name(StandardSegment::Debug).to_vec(),
        crate::EH_FRAME_SECTION_NAME.to_vec(),
        SectionKind::Other,
    );
    let data_offset = object.append_section_data(section, eh_frame_bytes, 4);

    // Add the personality pointer lazily. CIEs reference this slot indirectly
    // and PC-relative, while the slot itself has an absolute relocation to the
    // runtime function. This mirrors the `DW.ref.*` convention used by native
    // compilers and, unlike a GOT-relative data relocation, works across the
    // 64-bit ELF architectures supported by Wasmer.
    let mut personality_reference_symbol = None;
    for relocation in relocations {
        let symbol = match relocation.target {
            EhTarget::Function => function_symbol,
            EhTarget::Personality => {
                if let Some(symbol) = personality_reference_symbol {
                    symbol
                } else {
                    let personality_symbol = add_libcall_symbol(object, LibCall::EHPersonality);
                    let personality_section =
                        object.section_id(StandardSection::ReadOnlyDataWithRel);
                    let reference_symbol = object.add_symbol(Symbol {
                        name: b"DW.ref.wasmer_eh_personality".to_vec(),
                        value: 0,
                        size: 8,
                        kind: SymbolKind::Data,
                        scope: SymbolScope::Compilation,
                        weak: false,
                        section: SymbolSection::Undefined,
                        flags: SymbolFlags::None,
                    });
                    let reference_offset =
                        object.add_symbol_data(reference_symbol, personality_section, &[0; 8], 8);
                    object
                        .add_relocation(
                            personality_section,
                            ObjectRelocation {
                                offset: reference_offset,
                                flags: RelocationFlags::Generic {
                                    kind: ObjectRelocationKind::Absolute,
                                    encoding: RelocationEncoding::Generic,
                                    size: 64,
                                },
                                symbol: personality_symbol,
                                addend: 0,
                            },
                        )
                        .map_err(|e| {
                            CompileError::Codegen(format!(
                                "failed to add personality reference relocation: {e}"
                            ))
                        })?;
                    personality_reference_symbol = Some(reference_symbol);
                    reference_symbol
                }
            }
            EhTarget::Lsda => lsda_section_symbol.ok_or_else(|| {
                CompileError::Codegen(
                    ".eh_frame references an LSDA but none was emitted".to_string(),
                )
            })?,
        };
        object
            .add_relocation(
                section,
                ObjectRelocation {
                    offset: data_offset + relocation.offset,
                    flags: RelocationFlags::Generic {
                        kind: relocation.kind,
                        encoding: RelocationEncoding::Generic,
                        size: 8 * relocation.size,
                    },
                    symbol,
                    addend: relocation.addend,
                },
            )
            .map_err(|e| {
                CompileError::Codegen(format!("failed to add .eh_frame relocation: {e}"))
            })?;
    }
    Ok(())
}

/// Serialize a trampoline's body into its own relocatable object file.
pub fn emit_function_body(
    target: &Target,
    kind: &CompiledKind,
    body: &FunctionBody,
) -> Result<Vec<u8>, CompileError> {
    let mut object = get_object_for_target(target.triple())
        .map_err(|e| CompileError::Codegen(format!("cannot create object: {e}")))?;
    let symbol = object.add_symbol(Symbol {
        name: kind.linkage_name().into_bytes(),
        value: 0,
        size: body.body.len() as u64,
        kind: SymbolKind::Text,
        scope: SymbolScope::Linkage,
        weak: false,
        section: SymbolSection::Undefined,
        flags: SymbolFlags::None,
    });
    let text = object.section_id(StandardSection::Text);
    object.add_symbol_data(symbol, text, &body.body, 4);
    object
        .write()
        .map_err(|e| CompileError::Codegen(format!("failed to serialize object: {e}")))
}

/// Serialize an import trampoline's custom section into its own relocatable object file.
pub fn emit_import_trampoline(
    target: &Target,
    kind: &CompiledKind,
    section: &CustomSection,
) -> Result<Vec<u8>, CompileError> {
    let mut object = get_object_for_target(target.triple())
        .map_err(|e| CompileError::Codegen(format!("cannot create object: {e}")))?;
    let symbol = object.add_symbol(Symbol {
        name: kind.linkage_name().into_bytes(),
        value: 0,
        size: section.bytes.len() as u64,
        kind: SymbolKind::Text,
        scope: SymbolScope::Linkage,
        weak: false,
        section: SymbolSection::Undefined,
        flags: SymbolFlags::None,
    });
    let text = object.section_id(StandardSection::Text);
    object.add_symbol_data(symbol, text, section.bytes.as_slice(), 4);
    object
        .write()
        .map_err(|e| CompileError::Codegen(format!("failed to serialize object: {e}")))
}
/// Link all the per-function and trampoline objects (plus the Wasmer metadata
/// object) into the final shared-object module image.
#[allow(clippy::too_many_arguments)]
pub fn link_module(
    pool: &rayon::ThreadPool,
    target: &Target,
    compile_info_blob: &[u8],
    object_files: Vec<Vec<u8>>,
    import_trampoline_objects: Vec<Vec<u8>>,
    trampoline_objects: Vec<Vec<u8>>,
    dynamic_trampoline_objects: Vec<Vec<u8>>,
    debug_dir: Option<PathBuf>,
    module_hash: Option<String>,
    function_max_stack_usage: PrimaryMap<LocalFunctionIndex, Option<usize>>,
) -> Result<Compilation, CompileError> {
    let elf = emit_metadata_and_link(
        pool,
        target,
        compile_info_blob,
        CompiledObjects {
            object_files,
            import_trampoline_object_files: import_trampoline_objects,
            trampoline_object_files: trampoline_objects,
            dynamic_trampoline_object_files: dynamic_trampoline_objects,
        },
        debug_dir,
        module_hash,
    )?;
    Ok(Compilation::Elf {
        data: elf,
        function_max_stack_usage,
    })
}