wasmtime_cranelift/obj.rs
1//! Object file builder.
2//!
3//! Creates ELF image based on `Compilation` information. The ELF contains
4//! functions and trampolines in the ".text" section. It also contains all
5//! relocation records for the linking stage. If DWARF sections exist, their
6//! content will be written as well.
7//!
8//! The object file has symbols for each function and trampoline, as well as
9//! symbols that refer to libcalls.
10//!
11//! The function symbol names have format "_wasm_function_N", where N is
12//! `FuncIndex`. The defined wasm function symbols refer to a JIT compiled
13//! function body, the imported wasm function do not. The trampolines symbol
14//! names have format "_trampoline_N", where N is `SignatureIndex`.
15
16use crate::{CompiledFunction, RelocationTarget};
17use anyhow::Result;
18use cranelift_codegen::binemit::Reloc;
19use cranelift_codegen::isa::unwind::{systemv, UnwindInfo};
20use cranelift_codegen::TextSectionBuilder;
21use cranelift_control::ControlPlane;
22use gimli::write::{Address, EhFrame, EndianVec, FrameTable, Writer};
23use gimli::RunTimeEndian;
24use object::write::{Object, SectionId, StandardSegment, Symbol, SymbolId, SymbolSection};
25use object::{Architecture, SectionFlags, SectionKind, SymbolFlags, SymbolKind, SymbolScope};
26use std::collections::HashMap;
27use std::ops::Range;
28use wasmtime_environ::obj::{self, LibCall};
29use wasmtime_environ::{Compiler, TripleExt, Unsigned};
30
31const TEXT_SECTION_NAME: &[u8] = b".text";
32
33/// A helper structure used to assemble the final text section of an executable,
34/// plus unwinding information and other related details.
35///
36/// This builder relies on Cranelift-specific internals but assembles into a
37/// generic `Object` which will get further appended to in a compiler-agnostic
38/// fashion later.
39pub struct ModuleTextBuilder<'a> {
40 /// The target that we're compiling for, used to query target-specific
41 /// information as necessary.
42 compiler: &'a dyn Compiler,
43
44 /// The object file that we're generating code into.
45 obj: &'a mut Object<'static>,
46
47 /// The WebAssembly module we're generating code for.
48 text_section: SectionId,
49
50 unwind_info: UnwindInfoBuilder<'a>,
51
52 /// In-progress text section that we're using cranelift's `MachBuffer` to
53 /// build to resolve relocations (calls) between functions.
54 text: Box<dyn TextSectionBuilder>,
55
56 /// Symbols defined in the object for libcalls that relocations are applied
57 /// against.
58 ///
59 /// Note that this isn't typically used. It's only used for SSE-disabled
60 /// builds without SIMD on x86_64 right now.
61 libcall_symbols: HashMap<LibCall, SymbolId>,
62
63 ctrl_plane: ControlPlane,
64}
65
66impl<'a> ModuleTextBuilder<'a> {
67 /// Creates a new builder for the text section of an executable.
68 ///
69 /// The `.text` section will be appended to the specified `obj` along with
70 /// any unwinding or such information as necessary. The `num_funcs`
71 /// parameter indicates the number of times the `append_func` function will
72 /// be called. The `finish` function will panic if this contract is not met.
73 pub fn new(
74 obj: &'a mut Object<'static>,
75 compiler: &'a dyn Compiler,
76 text: Box<dyn TextSectionBuilder>,
77 ) -> Self {
78 // Entire code (functions and trampolines) will be placed
79 // in the ".text" section.
80 let text_section = obj.add_section(
81 obj.segment_name(StandardSegment::Text).to_vec(),
82 TEXT_SECTION_NAME.to_vec(),
83 SectionKind::Text,
84 );
85
86 // If this target is Pulley then flag the text section as not needing the
87 // executable bit in virtual memory which means that the runtime won't
88 // try to call `Mmap::make_executable`, which makes Pulley more
89 // portable.
90 if compiler.triple().is_pulley() {
91 let section = obj.section_mut(text_section);
92 assert!(matches!(section.flags, SectionFlags::None));
93 section.flags = SectionFlags::Elf {
94 sh_flags: obj::SH_WASMTIME_NOT_EXECUTED,
95 };
96 }
97
98 Self {
99 compiler,
100 obj,
101 text_section,
102 unwind_info: Default::default(),
103 text,
104 libcall_symbols: HashMap::default(),
105 ctrl_plane: ControlPlane::default(),
106 }
107 }
108
109 /// Appends the `func` specified named `name` to this object.
110 ///
111 /// The `resolve_reloc_target` closure is used to resolve a relocation
112 /// target to an adjacent function which has already been added or will be
113 /// added to this object. The argument is the relocation target specified
114 /// within `CompiledFunction` and the return value must be an index where
115 /// the target will be defined by the `n`th call to `append_func`.
116 ///
117 /// Returns the symbol associated with the function as well as the range
118 /// that the function resides within the text section.
119 pub fn append_func(
120 &mut self,
121 name: &str,
122 compiled_func: &'a CompiledFunction,
123 resolve_reloc_target: impl Fn(wasmtime_environ::RelocationTarget) -> usize,
124 ) -> (SymbolId, Range<u64>) {
125 let body = compiled_func.buffer.data();
126 let alignment = compiled_func.alignment;
127 let body_len = body.len() as u64;
128 let off = self
129 .text
130 .append(true, &body, alignment, &mut self.ctrl_plane);
131
132 let symbol_id = self.obj.add_symbol(Symbol {
133 name: name.as_bytes().to_vec(),
134 value: off,
135 size: body_len,
136 kind: SymbolKind::Text,
137 scope: SymbolScope::Compilation,
138 weak: false,
139 section: SymbolSection::Section(self.text_section),
140 flags: SymbolFlags::None,
141 });
142
143 if let Some(info) = compiled_func.unwind_info() {
144 self.unwind_info.push(off, body_len, info);
145 }
146
147 for r in compiled_func.relocations() {
148 let reloc_offset = off + u64::from(r.offset);
149 match r.reloc_target {
150 // Relocations against user-defined functions means that this is
151 // a relocation against a module-local function, typically a
152 // call between functions. The `text` field is given priority to
153 // resolve this relocation before we actually emit an object
154 // file, but if it can't handle it then we pass through the
155 // relocation.
156 RelocationTarget::Wasm(_) | RelocationTarget::Builtin(_) => {
157 let target = resolve_reloc_target(r.reloc_target);
158 if self
159 .text
160 .resolve_reloc(reloc_offset, r.reloc, r.addend, target)
161 {
162 continue;
163 }
164
165 // At this time it's expected that all relocations are
166 // handled by `text.resolve_reloc`, and anything that isn't
167 // handled is a bug in `text.resolve_reloc` or something
168 // transitively there. If truly necessary, though, then this
169 // loop could also be updated to forward the relocation to
170 // the final object file as well.
171 panic!(
172 "unresolved relocation could not be processed against \
173 {:?}: {r:?}",
174 r.reloc_target,
175 );
176 }
177
178 // Relocations against libcalls are not common at this time and
179 // are only used in non-default configurations that disable wasm
180 // SIMD, disable SSE features, and for wasm modules that still
181 // use floating point operations.
182 //
183 // Currently these relocations are all expected to be absolute
184 // 8-byte relocations so that's asserted here and then encoded
185 // directly into the object as a normal object relocation. This
186 // is processed at module load time to resolve the relocations.
187 RelocationTarget::HostLibcall(call) => {
188 let symbol = *self.libcall_symbols.entry(call).or_insert_with(|| {
189 self.obj.add_symbol(Symbol {
190 name: call.symbol().as_bytes().to_vec(),
191 value: 0,
192 size: 0,
193 kind: SymbolKind::Text,
194 scope: SymbolScope::Linkage,
195 weak: false,
196 section: SymbolSection::Undefined,
197 flags: SymbolFlags::None,
198 })
199 });
200 let flags = match r.reloc {
201 Reloc::Abs8 => object::RelocationFlags::Generic {
202 encoding: object::RelocationEncoding::Generic,
203 kind: object::RelocationKind::Absolute,
204 size: 64,
205 },
206 other => unimplemented!("unimplemented relocation kind {other:?}"),
207 };
208 self.obj
209 .add_relocation(
210 self.text_section,
211 object::write::Relocation {
212 symbol,
213 flags,
214 offset: reloc_offset,
215 addend: r.addend,
216 },
217 )
218 .unwrap();
219 }
220
221 // This relocation is used to fill in which hostcall id is
222 // desired within the `call_indirect_host` opcode of Pulley
223 // itself. The relocation target is the start of the instruction
224 // and the goal is to insert the static signature number, `n`,
225 // into the instruction.
226 //
227 // At this time the instruction looks like:
228 //
229 // +------+------+------+------+
230 // | OP | OP_EXTENDED | N |
231 // +------+------+------+------+
232 //
233 // This 4-byte encoding has `OP` indicating this is an "extended
234 // opcode" where `OP_EXTENDED` is a 16-bit extended opcode.
235 // The `N` byte is the index of the signature being called and
236 // is what's b eing filled in.
237 //
238 // See the `test_call_indirect_host_width` in
239 // `pulley/tests/all.rs` for this guarantee as well.
240 RelocationTarget::PulleyHostcall(n) => {
241 #[cfg(feature = "pulley")]
242 {
243 use pulley_interpreter::encode::Encode;
244 assert_eq!(pulley_interpreter::CallIndirectHost::WIDTH, 4);
245 }
246 let byte = u8::try_from(n).unwrap();
247 self.text.write(reloc_offset + 3, &[byte]);
248 }
249 };
250 }
251 (symbol_id, off..off + body_len)
252 }
253
254 /// Forces "veneers" to be used for inter-function calls in the text
255 /// section which means that in-bounds optimized addresses are never used.
256 ///
257 /// This is only useful for debugging cranelift itself and typically this
258 /// option is disabled.
259 pub fn force_veneers(&mut self) {
260 self.text.force_veneers();
261 }
262
263 /// Appends the specified amount of bytes of padding into the text section.
264 ///
265 /// This is only useful when fuzzing and/or debugging cranelift itself and
266 /// for production scenarios `padding` is 0 and this function does nothing.
267 pub fn append_padding(&mut self, padding: usize) {
268 if padding == 0 {
269 return;
270 }
271 self.text
272 .append(false, &vec![0; padding], 1, &mut self.ctrl_plane);
273 }
274
275 /// Indicates that the text section has been written completely and this
276 /// will finish appending it to the original object.
277 ///
278 /// Note that this will also write out the unwind information sections if
279 /// necessary.
280 pub fn finish(mut self) {
281 // Finish up the text section now that we're done adding functions.
282 let text = self.text.finish(&mut self.ctrl_plane);
283 self.obj
284 .section_mut(self.text_section)
285 .set_data(text, self.compiler.page_size_align());
286
287 // Append the unwind information for all our functions, if necessary.
288 self.unwind_info
289 .append_section(self.compiler, self.obj, self.text_section);
290 }
291}
292
293/// Builder used to create unwind information for a set of functions added to a
294/// text section.
295#[derive(Default)]
296struct UnwindInfoBuilder<'a> {
297 windows_xdata: Vec<u8>,
298 windows_pdata: Vec<RUNTIME_FUNCTION>,
299 systemv_unwind_info: Vec<(u64, &'a systemv::UnwindInfo)>,
300}
301
302// This is a mirror of `RUNTIME_FUNCTION` in the Windows API, but defined here
303// to ensure everything is always `u32` and to have it available on all
304// platforms. Note that all of these specifiers here are relative to a "base
305// address" which we define as the base of where the text section is eventually
306// loaded.
307#[expect(non_camel_case_types, reason = "matching Windows style, not Rust")]
308struct RUNTIME_FUNCTION {
309 begin: u32,
310 end: u32,
311 unwind_address: u32,
312}
313
314impl<'a> UnwindInfoBuilder<'a> {
315 /// Pushes the unwind information for a function into this builder.
316 ///
317 /// The function being described must be located at `function_offset` within
318 /// the text section itself, and the function's size is specified by
319 /// `function_len`.
320 ///
321 /// The `info` should come from Cranelift. and is handled here depending on
322 /// its flavor.
323 fn push(&mut self, function_offset: u64, function_len: u64, info: &'a UnwindInfo) {
324 match info {
325 // Windows unwind information is stored in two locations:
326 //
327 // * First is the actual unwinding information which is stored
328 // in the `.xdata` section. This is where `info`'s emitted
329 // information will go into.
330 // * Second are pointers to connect all this unwind information,
331 // stored in the `.pdata` section. The `.pdata` section is an
332 // array of `RUNTIME_FUNCTION` structures.
333 //
334 // Due to how these will be loaded at runtime the `.pdata` isn't
335 // actually assembled byte-wise here. Instead that's deferred to
336 // happen later during `write_windows_unwind_info` which will apply
337 // a further offset to `unwind_address`.
338 //
339 // FIXME: in theory we could "intern" the `unwind_info` value
340 // here within the `.xdata` section. Most of our unwind
341 // information for functions is probably pretty similar in which
342 // case the `.xdata` could be quite small and `.pdata` could
343 // have multiple functions point to the same unwinding
344 // information.
345 UnwindInfo::WindowsX64(info) => {
346 let unwind_size = info.emit_size();
347 let mut unwind_info = vec![0; unwind_size];
348 info.emit(&mut unwind_info);
349
350 // `.xdata` entries are always 4-byte aligned
351 while self.windows_xdata.len() % 4 != 0 {
352 self.windows_xdata.push(0x00);
353 }
354 let unwind_address = self.windows_xdata.len();
355 self.windows_xdata.extend_from_slice(&unwind_info);
356
357 // Record a `RUNTIME_FUNCTION` which this will point to.
358 self.windows_pdata.push(RUNTIME_FUNCTION {
359 begin: u32::try_from(function_offset).unwrap(),
360 end: u32::try_from(function_offset + function_len).unwrap(),
361 unwind_address: u32::try_from(unwind_address).unwrap(),
362 });
363 }
364
365 // See https://learn.microsoft.com/en-us/cpp/build/arm64-exception-handling
366 UnwindInfo::WindowsArm64(info) => {
367 let code_words = info.code_words();
368 let mut unwind_codes = vec![0; (code_words * 4) as usize];
369 info.emit(&mut unwind_codes);
370
371 // `.xdata` entries are always 4-byte aligned
372 while self.windows_xdata.len() % 4 != 0 {
373 self.windows_xdata.push(0x00);
374 }
375
376 // First word:
377 // 0-17: Function Length
378 // 18-19: Version (must be 0)
379 // 20: X bit (is exception data present?)
380 // 21: E bit (has single packed epilogue?)
381 // 22-26: Epilogue count
382 // 27-31: Code words count
383 let requires_extended_counts = code_words > (1 << 5);
384 let encoded_function_len = function_len / 4;
385 assert!(encoded_function_len < (1 << 18), "function too large");
386 let mut word1 = u32::try_from(encoded_function_len).unwrap();
387 if !requires_extended_counts {
388 word1 |= u32::from(code_words) << 27;
389 }
390 let unwind_address = self.windows_xdata.len();
391 self.windows_xdata.extend_from_slice(&word1.to_le_bytes());
392
393 if requires_extended_counts {
394 // Extended counts word:
395 // 0-15: Epilogue count
396 // 16-23: Code words count
397 let extended_counts_word = (code_words as u32) << 16;
398 self.windows_xdata
399 .extend_from_slice(&extended_counts_word.to_le_bytes());
400 }
401
402 // Skip epilogue information: Per comment on [`UnwindInst`], we
403 // do not emit information about epilogues.
404
405 // Emit the unwind codes.
406 self.windows_xdata.extend_from_slice(&unwind_codes);
407
408 // Record a `RUNTIME_FUNCTION` which this will point to.
409 // NOTE: `end` is not used, so leave it as 0.
410 self.windows_pdata.push(RUNTIME_FUNCTION {
411 begin: u32::try_from(function_offset).unwrap(),
412 end: 0,
413 unwind_address: u32::try_from(unwind_address).unwrap(),
414 });
415 }
416
417 // System-V is different enough that we just record the unwinding
418 // information to get processed at a later time.
419 UnwindInfo::SystemV(info) => {
420 self.systemv_unwind_info.push((function_offset, info));
421 }
422
423 _ => panic!("some unwind info isn't handled here"),
424 }
425 }
426
427 /// Appends the unwind information section, if any, to the `obj` specified.
428 ///
429 /// This function must be called immediately after the text section was
430 /// added to a builder. The unwind information section must trail the text
431 /// section immediately.
432 ///
433 /// The `text_section`'s section identifier is passed into this function.
434 fn append_section(
435 &self,
436 compiler: &dyn Compiler,
437 obj: &mut Object<'_>,
438 text_section: SectionId,
439 ) {
440 // This write will align the text section to a page boundary and then
441 // return the offset at that point. This gives us the full size of the
442 // text section at that point, after alignment.
443 let text_section_size =
444 obj.append_section_data(text_section, &[], compiler.page_size_align());
445
446 if self.windows_xdata.len() > 0 {
447 assert!(self.systemv_unwind_info.len() == 0);
448 // The `.xdata` section must come first to be just-after the `.text`
449 // section for the reasons documented in `write_windows_unwind_info`
450 // below.
451 let segment = obj.segment_name(StandardSegment::Data).to_vec();
452 let xdata_id = obj.add_section(segment, b".xdata".to_vec(), SectionKind::ReadOnlyData);
453 let segment = obj.segment_name(StandardSegment::Data).to_vec();
454 let pdata_id = obj.add_section(segment, b".pdata".to_vec(), SectionKind::ReadOnlyData);
455 self.write_windows_unwind_info(obj, xdata_id, pdata_id, text_section_size);
456 }
457
458 if self.systemv_unwind_info.len() > 0 {
459 let segment = obj.segment_name(StandardSegment::Data).to_vec();
460 let section_id =
461 obj.add_section(segment, b".eh_frame".to_vec(), SectionKind::ReadOnlyData);
462 self.write_systemv_unwind_info(compiler, obj, section_id, text_section_size)
463 }
464 }
465
466 /// This function appends a nonstandard section to the object which is only
467 /// used during `CodeMemory::publish`.
468 ///
469 /// This custom section effectively stores a `[RUNTIME_FUNCTION; N]` into
470 /// the object file itself. This way registration of unwind info can simply
471 /// pass this slice to the OS itself and there's no need to recalculate
472 /// anything on the other end of loading a module from a precompiled object.
473 ///
474 /// Support for reading this is in `crates/jit/src/unwind/winx64.rs`.
475 fn write_windows_unwind_info(
476 &self,
477 obj: &mut Object<'_>,
478 xdata_id: SectionId,
479 pdata_id: SectionId,
480 text_section_size: u64,
481 ) {
482 // Append the `.xdata` section, or the actual unwinding information
483 // codes and such which were built as we found unwind information for
484 // functions.
485 obj.append_section_data(xdata_id, &self.windows_xdata, 4);
486
487 // Next append the `.pdata` section, or the array of `RUNTIME_FUNCTION`
488 // structures stored in the binary.
489 //
490 // This memory will be passed at runtime to `RtlAddFunctionTable` which
491 // takes a "base address" and the entries within `RUNTIME_FUNCTION` are
492 // all relative to this base address. The base address we pass is the
493 // address of the text section itself so all the pointers here must be
494 // text-section-relative. The `begin` and `end` fields for the function
495 // it describes are already text-section-relative, but the
496 // `unwind_address` field needs to be updated here since the value
497 // stored right now is `xdata`-section-relative. We know that the
498 // `xdata` section follows the `.text` section so the
499 // `text_section_size` is added in to calculate the final
500 // `.text`-section-relative address of the unwind information.
501 let xdata_rva = |address| {
502 let address = u64::from(address);
503 let address = address + text_section_size;
504 u32::try_from(address).unwrap()
505 };
506 let pdata = match obj.architecture() {
507 Architecture::X86_64 => {
508 let mut pdata = Vec::with_capacity(self.windows_pdata.len() * 3 * 4);
509 for info in self.windows_pdata.iter() {
510 pdata.extend_from_slice(&info.begin.to_le_bytes());
511 pdata.extend_from_slice(&info.end.to_le_bytes());
512 pdata.extend_from_slice(&xdata_rva(info.unwind_address).to_le_bytes());
513 }
514 pdata
515 }
516
517 Architecture::Aarch64 => {
518 // Windows Arm64 .pdata also supports packed unwind data, but
519 // we're not currently using that.
520 let mut pdata = Vec::with_capacity(self.windows_pdata.len() * 2 * 4);
521 for info in self.windows_pdata.iter() {
522 pdata.extend_from_slice(&info.begin.to_le_bytes());
523 pdata.extend_from_slice(&xdata_rva(info.unwind_address).to_le_bytes());
524 }
525 pdata
526 }
527
528 _ => unimplemented!("unsupported architecture for windows unwind info"),
529 };
530 obj.append_section_data(pdata_id, &pdata, 4);
531 }
532
533 /// This function appends a nonstandard section to the object which is only
534 /// used during `CodeMemory::publish`.
535 ///
536 /// This will generate a `.eh_frame` section, but not one that can be
537 /// naively loaded. The goal of this section is that we can create the
538 /// section once here and never again does it need to change. To describe
539 /// dynamically loaded functions though each individual FDE needs to talk
540 /// about the function's absolute address that it's referencing. Naturally
541 /// we don't actually know the function's absolute address when we're
542 /// creating an object here.
543 ///
544 /// To solve this problem the FDE address encoding mode is set to
545 /// `DW_EH_PE_pcrel`. This means that the actual effective address that the
546 /// FDE describes is a relative to the address of the FDE itself. By
547 /// leveraging this relative-ness we can assume that the relative distance
548 /// between the FDE and the function it describes is constant, which should
549 /// allow us to generate an FDE ahead-of-time here.
550 ///
551 /// For now this assumes that all the code of functions will start at a
552 /// page-aligned address when loaded into memory. The eh_frame encoded here
553 /// then assumes that the text section is itself page aligned to its size
554 /// and the eh_frame will follow just after the text section. This means
555 /// that the relative offsets we're using here is the FDE going backwards
556 /// into the text section itself.
557 ///
558 /// Note that the library we're using to create the FDEs, `gimli`, doesn't
559 /// actually encode addresses relative to the FDE itself. Instead the
560 /// addresses are encoded relative to the start of the `.eh_frame` section.
561 /// This makes it much easier for us where we provide the relative offset
562 /// from the start of `.eh_frame` to the function in the text section, which
563 /// given our layout basically means the offset of the function in the text
564 /// section from the end of the text section.
565 ///
566 /// A final note is that the reason we page-align the text section's size is
567 /// so the .eh_frame lives on a separate page from the text section itself.
568 /// This allows `.eh_frame` to have different virtual memory permissions,
569 /// such as being purely read-only instead of read/execute like the code
570 /// bits.
571 fn write_systemv_unwind_info(
572 &self,
573 compiler: &dyn Compiler,
574 obj: &mut Object<'_>,
575 section_id: SectionId,
576 text_section_size: u64,
577 ) {
578 let mut cie = match compiler.create_systemv_cie() {
579 Some(cie) => cie,
580 None => return,
581 };
582 let mut table = FrameTable::default();
583 cie.fde_address_encoding = gimli::constants::DW_EH_PE_pcrel;
584 let cie_id = table.add_cie(cie);
585
586 for (text_section_off, unwind_info) in self.systemv_unwind_info.iter() {
587 let backwards_off = text_section_size - text_section_off;
588 let actual_offset = -i64::try_from(backwards_off).unwrap();
589 // Note that gimli wants an unsigned 64-bit integer here, but
590 // unwinders just use this constant for a relative addition with the
591 // address of the FDE, which means that the sign doesn't actually
592 // matter.
593 let fde = unwind_info.to_fde(Address::Constant(actual_offset.unsigned()));
594 table.add_fde(cie_id, fde);
595 }
596 let endian = match compiler.triple().endianness().unwrap() {
597 target_lexicon::Endianness::Little => RunTimeEndian::Little,
598 target_lexicon::Endianness::Big => RunTimeEndian::Big,
599 };
600 let mut eh_frame = EhFrame(MyVec(EndianVec::new(endian)));
601 table.write_eh_frame(&mut eh_frame).unwrap();
602
603 // Some unwinding implementations expect a terminating "empty" length so
604 // a 0 is written at the end of the table for those implementations.
605 let mut endian_vec = (eh_frame.0).0;
606 endian_vec.write_u32(0).unwrap();
607 obj.append_section_data(section_id, endian_vec.slice(), 1);
608
609 use gimli::constants;
610 use gimli::write::Error;
611
612 struct MyVec(EndianVec<RunTimeEndian>);
613
614 impl Writer for MyVec {
615 type Endian = RunTimeEndian;
616
617 fn endian(&self) -> RunTimeEndian {
618 self.0.endian()
619 }
620
621 fn len(&self) -> usize {
622 self.0.len()
623 }
624
625 fn write(&mut self, buf: &[u8]) -> Result<(), Error> {
626 self.0.write(buf)
627 }
628
629 fn write_at(&mut self, pos: usize, buf: &[u8]) -> Result<(), Error> {
630 self.0.write_at(pos, buf)
631 }
632
633 // FIXME(gimli-rs/gimli#576) this is the definition we want for
634 // `write_eh_pointer` but the default implementation, at the time
635 // of this writing, uses `offset - val` instead of `val - offset`.
636 // A PR has been merged to fix this but until that's published we
637 // can't use it.
638 fn write_eh_pointer(
639 &mut self,
640 address: Address,
641 eh_pe: constants::DwEhPe,
642 size: u8,
643 ) -> Result<(), Error> {
644 let val = match address {
645 Address::Constant(val) => val,
646 Address::Symbol { .. } => unreachable!(),
647 };
648 assert_eq!(eh_pe.application(), constants::DW_EH_PE_pcrel);
649 let offset = self.len() as u64;
650 let val = val.wrapping_sub(offset);
651 self.write_eh_pointer_data(val, eh_pe.format(), size)
652 }
653 }
654 }
655}