From d0706e6f715949305f5bf55385a18598b13ceb18 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 9 Nov 2020 09:39:36 +0100 Subject: [PATCH 01/10] Merge finalize_function and perform_relocations --- cranelift/simplejit/src/backend.rs | 62 ++++++------------------------ 1 file changed, 11 insertions(+), 51 deletions(-) diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index 527aadf0cb..bfde77aa6d 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -291,13 +291,9 @@ impl SimpleJITModule { } } - fn finalize_function(&mut self, id: FuncId) { + fn perform_relocations(&self, blob: &CompiledBlob) { use std::ptr::write_unaligned; - let func = self.functions[id] - .as_ref() - .expect("function must be compiled before it can be finalized"); - for &RelocRecord { reloc, offset, @@ -305,8 +301,8 @@ impl SimpleJITModule { addend, } in &func.relocs { - debug_assert!((offset as usize) < func.size); - let at = unsafe { func.ptr.offset(offset as isize) }; + debug_assert!((offset as usize) < blob.size); + let at = unsafe { blob.ptr.offset(offset as isize) }; let base = self.get_definition(name); // TODO: Handle overflow. let what = unsafe { base.offset(addend as isize) }; @@ -338,48 +334,6 @@ impl SimpleJITModule { } } - fn finalize_data(&mut self, id: DataId) { - use std::ptr::write_unaligned; - - let data = self.data_objects[id] - .as_ref() - .expect("data object must be compiled before it can be finalized"); - - for &RelocRecord { - reloc, - offset, - ref name, - addend, - } in &data.relocs - { - debug_assert!((offset as usize) < data.size); - let at = unsafe { data.ptr.offset(offset as isize) }; - let base = self.get_definition(name); - // TODO: Handle overflow. - let what = unsafe { base.offset(addend as isize) }; - match reloc { - Reloc::Abs4 => { - // TODO: Handle overflow. - #[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_ptr_alignment))] - unsafe { - write_unaligned(at as *mut u32, what as u32) - }; - } - Reloc::Abs8 => { - #[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_ptr_alignment))] - unsafe { - write_unaligned(at as *mut u64, what as u64) - }; - } - Reloc::X86PCRel4 - | Reloc::X86CallPCRel4 - | Reloc::X86GOTPCRel4 - | Reloc::X86CallPLTRel4 => panic!("unexpected text relocation in data"), - _ => unimplemented!(), - } - } - } - /// Finalize all functions and data objects that are defined but not yet finalized. /// All symbols referenced in their bodies that are declared as needing a definition /// must be defined by this point. @@ -390,12 +344,18 @@ impl SimpleJITModule { for func in std::mem::take(&mut self.functions_to_finalize) { let decl = self.declarations.get_function_decl(func); debug_assert!(decl.linkage.is_definable()); - self.finalize_function(func); + let func = self.functions[func] + .as_ref() + .expect("function must be compiled before it can be finalized"); + self.perform_relocations(func); } for data in std::mem::take(&mut self.data_objects_to_finalize) { let decl = self.declarations.get_data_decl(data); debug_assert!(decl.linkage.is_definable()); - self.finalize_data(data); + let data = self.data_objects[data] + .as_ref() + .expect("data object must be compiled before it can be finalized"); + self.perform_relocations(data); } // Now that we're done patching, prepare the memory for execution! From 2ca2255a4a5947c7c903896aecc57e1dffde8aeb Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 9 Nov 2020 09:51:49 +0100 Subject: [PATCH 02/10] Move CompiledBlob to a new file --- cranelift/simplejit/src/backend.rs | 56 ++---------------------- cranelift/simplejit/src/compiled_blob.rs | 55 +++++++++++++++++++++++ cranelift/simplejit/src/lib.rs | 1 + 3 files changed, 59 insertions(+), 53 deletions(-) create mode 100644 cranelift/simplejit/src/compiled_blob.rs diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index bfde77aa6d..8d2b5dd0c0 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -1,6 +1,6 @@ //! Defines `SimpleJITModule`. -use crate::memory::Memory; +use crate::{compiled_blob::CompiledBlob, memory::Memory}; use cranelift_codegen::binemit::{ Addend, CodeInfo, CodeOffset, Reloc, RelocSink, StackMap, StackMapSink, TrapSink, }; @@ -141,13 +141,6 @@ struct StackMapRecord { stack_map: StackMap, } -#[derive(Clone)] -struct CompiledBlob { - ptr: *mut u8, - size: usize, - relocs: Vec, -} - /// A handle to allow freeing memory allocated by the `Module`. struct MemoryHandle { code: Memory, @@ -291,49 +284,6 @@ impl SimpleJITModule { } } - fn perform_relocations(&self, blob: &CompiledBlob) { - use std::ptr::write_unaligned; - - for &RelocRecord { - reloc, - offset, - ref name, - addend, - } in &func.relocs - { - debug_assert!((offset as usize) < blob.size); - let at = unsafe { blob.ptr.offset(offset as isize) }; - let base = self.get_definition(name); - // TODO: Handle overflow. - let what = unsafe { base.offset(addend as isize) }; - match reloc { - Reloc::Abs4 => { - // TODO: Handle overflow. - #[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_ptr_alignment))] - unsafe { - write_unaligned(at as *mut u32, what as u32) - }; - } - Reloc::Abs8 => { - #[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_ptr_alignment))] - unsafe { - write_unaligned(at as *mut u64, what as u64) - }; - } - Reloc::X86PCRel4 | Reloc::X86CallPCRel4 => { - // TODO: Handle overflow. - let pcrel = ((what as isize) - (at as isize)) as i32; - #[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_ptr_alignment))] - unsafe { - write_unaligned(at as *mut i32, pcrel) - }; - } - Reloc::X86GOTPCRel4 | Reloc::X86CallPLTRel4 => panic!("unexpected PIC relocation"), - _ => unimplemented!(), - } - } - } - /// Finalize all functions and data objects that are defined but not yet finalized. /// All symbols referenced in their bodies that are declared as needing a definition /// must be defined by this point. @@ -347,7 +297,7 @@ impl SimpleJITModule { let func = self.functions[func] .as_ref() .expect("function must be compiled before it can be finalized"); - self.perform_relocations(func); + func.perform_relocations(|name| self.get_definition(name)); } for data in std::mem::take(&mut self.data_objects_to_finalize) { let decl = self.declarations.get_data_decl(data); @@ -355,7 +305,7 @@ impl SimpleJITModule { let data = self.data_objects[data] .as_ref() .expect("data object must be compiled before it can be finalized"); - self.perform_relocations(data); + data.perform_relocations(|name| self.get_definition(name)); } // Now that we're done patching, prepare the memory for execution! diff --git a/cranelift/simplejit/src/compiled_blob.rs b/cranelift/simplejit/src/compiled_blob.rs new file mode 100644 index 0000000000..dc8563dabd --- /dev/null +++ b/cranelift/simplejit/src/compiled_blob.rs @@ -0,0 +1,55 @@ +use cranelift_codegen::binemit::Reloc; +use cranelift_codegen::ir::ExternalName; +use cranelift_module::RelocRecord; + +#[derive(Clone)] +pub(crate) struct CompiledBlob { + pub(crate) ptr: *mut u8, + pub(crate) size: usize, + pub(crate) relocs: Vec, +} + +impl CompiledBlob { + pub(crate) fn perform_relocations(&self, get_definition: impl Fn(&ExternalName) -> *const u8) { + use std::ptr::write_unaligned; + + for &RelocRecord { + reloc, + offset, + ref name, + addend, + } in &self.relocs + { + debug_assert!((offset as usize) < self.size); + let at = unsafe { self.ptr.offset(offset as isize) }; + let base = get_definition(name); + // TODO: Handle overflow. + let what = unsafe { base.offset(addend as isize) }; + match reloc { + Reloc::Abs4 => { + // TODO: Handle overflow. + #[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_ptr_alignment))] + unsafe { + write_unaligned(at as *mut u32, what as u32) + }; + } + Reloc::Abs8 => { + #[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_ptr_alignment))] + unsafe { + write_unaligned(at as *mut u64, what as u64) + }; + } + Reloc::X86PCRel4 | Reloc::X86CallPCRel4 => { + // TODO: Handle overflow. + let pcrel = ((what as isize) - (at as isize)) as i32; + #[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_ptr_alignment))] + unsafe { + write_unaligned(at as *mut i32, pcrel) + }; + } + Reloc::X86GOTPCRel4 | Reloc::X86CallPLTRel4 => panic!("unexpected PIC relocation"), + _ => unimplemented!(), + } + } + } +} diff --git a/cranelift/simplejit/src/lib.rs b/cranelift/simplejit/src/lib.rs index cb933bae51..208106b93d 100644 --- a/cranelift/simplejit/src/lib.rs +++ b/cranelift/simplejit/src/lib.rs @@ -23,6 +23,7 @@ )] mod backend; +mod compiled_blob; mod memory; pub use crate::backend::{SimpleJITBuilder, SimpleJITModule, SimpleJITProduct}; From 856f799adef59bfa3cfacab70f316da4088149ad Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 9 Nov 2020 10:07:18 +0100 Subject: [PATCH 03/10] Make some things more consistent between define_function and define_function_bytes --- cranelift/module/src/module.rs | 3 --- cranelift/object/src/backend.rs | 16 ++++++++-------- cranelift/simplejit/src/backend.rs | 28 ++++++++++++++-------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/cranelift/module/src/module.rs b/cranelift/module/src/module.rs index b1ef1024a2..7112c1e86c 100644 --- a/cranelift/module/src/module.rs +++ b/cranelift/module/src/module.rs @@ -162,9 +162,6 @@ pub enum ModuleError { /// Indicates an identifier was defined, but was declared as an import #[error("Invalid to define identifier declared as an import: {0}")] InvalidImportDefinition(String), - /// Indicates a too-long function was defined - #[error("Function {0} exceeds the maximum function size")] - FunctionTooLarge(String), /// Wraps a `cranelift-codegen` error #[error("Compilation error: {0}")] Compilation(#[from] CodegenError), diff --git a/cranelift/object/src/backend.rs b/cranelift/object/src/backend.rs index 6c18b2f22e..e27ebcde04 100644 --- a/cranelift/object/src/backend.rs +++ b/cranelift/object/src/backend.rs @@ -1,12 +1,13 @@ //! Defines `ObjectModule`. use anyhow::anyhow; -use cranelift_codegen::binemit::{ - Addend, CodeInfo, CodeOffset, NullStackMapSink, Reloc, RelocSink, TrapSink, -}; use cranelift_codegen::entity::SecondaryMap; use cranelift_codegen::isa::TargetIsa; use cranelift_codegen::{self, ir}; +use cranelift_codegen::{ + binemit::{Addend, CodeInfo, CodeOffset, NullStackMapSink, Reloc, RelocSink, TrapSink}, + CodegenError, +}; use cranelift_module::{ DataContext, DataDescription, DataId, FuncId, Init, Linkage, Module, ModuleCompiledFunction, ModuleDeclarations, ModuleError, ModuleResult, RelocRecord, @@ -268,17 +269,16 @@ impl Module for ObjectModule { relocs: &[RelocRecord], ) -> ModuleResult { info!("defining function {} with bytes", func_id); + let total_size: u32 = match bytes.len().try_into() { + Ok(total_size) => total_size, + _ => Err(CodegenError::CodeTooLarge)?, + }; let decl = self.declarations.get_function_decl(func_id); if !decl.linkage.is_definable() { return Err(ModuleError::InvalidImportDefinition(decl.name.clone())); } - let total_size: u32 = match bytes.len().try_into() { - Ok(total_size) => total_size, - _ => Err(ModuleError::FunctionTooLarge(decl.name.clone()))?, - }; - let &mut (symbol, ref mut defined) = self.functions[func_id].as_mut().unwrap(); if *defined { return Err(ModuleError::DuplicateDefinition(decl.name.clone())); diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index 8d2b5dd0c0..eb037154dc 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -1,12 +1,13 @@ //! Defines `SimpleJITModule`. use crate::{compiled_blob::CompiledBlob, memory::Memory}; -use cranelift_codegen::binemit::{ - Addend, CodeInfo, CodeOffset, Reloc, RelocSink, StackMap, StackMapSink, TrapSink, -}; use cranelift_codegen::isa::TargetIsa; use cranelift_codegen::settings::Configurable; use cranelift_codegen::{self, ir, settings}; +use cranelift_codegen::{ + binemit::{Addend, CodeInfo, CodeOffset, Reloc, RelocSink, StackMap, StackMapSink, TrapSink}, + CodegenError, +}; use cranelift_entity::SecondaryMap; use cranelift_module::{ DataContext, DataDescription, DataId, FuncId, FuncOrDataId, Init, Linkage, Module, @@ -394,7 +395,6 @@ impl<'simple_jit_backend> Module for SimpleJITModule { return Err(ModuleError::DuplicateDefinition(decl.name.to_owned())); } - self.functions_to_finalize.push(id); let size = code_size as usize; let ptr = self .memory @@ -402,8 +402,6 @@ impl<'simple_jit_backend> Module for SimpleJITModule { .allocate(size, EXECUTABLE_DATA_ALIGNMENT) .expect("TODO: handle OOM etc."); - self.record_function_for_perf(ptr, size, &decl.name); - let mut reloc_sink = SimpleJITRelocSink::default(); let mut stack_map_sink = SimpleJITStackMapSink::default(); unsafe { @@ -416,11 +414,13 @@ impl<'simple_jit_backend> Module for SimpleJITModule { ) }; + self.record_function_for_perf(ptr, size, &decl.name); self.functions[id] = Some(CompiledBlob { ptr, size, relocs: reloc_sink.relocs, }); + self.functions_to_finalize.push(id); Ok(ModuleCompiledFunction { size: code_size }) } @@ -431,21 +431,21 @@ impl<'simple_jit_backend> Module for SimpleJITModule { bytes: &[u8], relocs: &[RelocRecord], ) -> ModuleResult { + info!("defining function {} with bytes", id); + let total_size: u32 = match bytes.len().try_into() { + Ok(total_size) => total_size, + _ => Err(CodegenError::CodeTooLarge)?, + }; + let decl = self.declarations.get_function_decl(id); if !decl.linkage.is_definable() { return Err(ModuleError::InvalidImportDefinition(decl.name.clone())); } - let total_size: u32 = match bytes.len().try_into() { - Ok(total_size) => total_size, - _ => Err(ModuleError::FunctionTooLarge(decl.name.clone()))?, - }; - if !self.functions[id].is_none() { return Err(ModuleError::DuplicateDefinition(decl.name.to_owned())); } - self.functions_to_finalize.push(id); let size = bytes.len(); let ptr = self .memory @@ -453,17 +453,17 @@ impl<'simple_jit_backend> Module for SimpleJITModule { .allocate(size, EXECUTABLE_DATA_ALIGNMENT) .expect("TODO: handle OOM etc."); - self.record_function_for_perf(ptr, size, &decl.name); - unsafe { ptr::copy_nonoverlapping(bytes.as_ptr(), ptr, size); } + self.record_function_for_perf(ptr, size, &decl.name); self.functions[id] = Some(CompiledBlob { ptr, size, relocs: relocs.to_vec(), }); + self.functions_to_finalize.push(id); Ok(ModuleCompiledFunction { size: total_size }) } From d5d5cba8b252e26933adddb9bf6a235ec67074ad Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 9 Nov 2020 10:11:19 +0100 Subject: [PATCH 04/10] Remove SimpleJITProduct All of it's methods now have an equivalent on SimpleJitModule --- .../simplejit/examples/simplejit-minimal.rs | 4 +- cranelift/simplejit/src/backend.rs | 58 +------------------ cranelift/simplejit/src/lib.rs | 2 +- cranelift/simplejit/tests/basic.rs | 2 +- 4 files changed, 7 insertions(+), 59 deletions(-) diff --git a/cranelift/simplejit/examples/simplejit-minimal.rs b/cranelift/simplejit/examples/simplejit-minimal.rs index 7c5df36eee..ae06c07b95 100644 --- a/cranelift/simplejit/examples/simplejit-minimal.rs +++ b/cranelift/simplejit/examples/simplejit-minimal.rs @@ -70,10 +70,10 @@ fn main() { module.clear_context(&mut ctx); // Perform linking. - let product = module.finish(); + module.finalize_definitions(); // Get a raw pointer to the generated code. - let code_b = product.lookup_func(func_b); + let code_b = module.get_finalized_function(func_b); // Cast it to a rust function pointer type. let ptr_b = unsafe { mem::transmute::<_, fn() -> u32>(code_b) }; diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index eb037154dc..72e49bd02f 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -10,8 +10,8 @@ use cranelift_codegen::{ }; use cranelift_entity::SecondaryMap; use cranelift_module::{ - DataContext, DataDescription, DataId, FuncId, FuncOrDataId, Init, Linkage, Module, - ModuleCompiledFunction, ModuleDeclarations, ModuleError, ModuleResult, RelocRecord, + DataContext, DataDescription, DataId, FuncId, Init, Linkage, Module, ModuleCompiledFunction, + ModuleDeclarations, ModuleError, ModuleResult, RelocRecord, }; use cranelift_native; #[cfg(not(windows))] @@ -149,16 +149,7 @@ struct MemoryHandle { writable: Memory, } -/// A `SimpleJITProduct` allows looking up the addresses of all functions and data objects -/// defined in the original module. -pub struct SimpleJITProduct { - memory: MemoryHandle, - declarations: ModuleDeclarations, - functions: SecondaryMap>, - data_objects: SecondaryMap>, -} - -impl SimpleJITProduct { +impl SimpleJITModule { /// Free memory allocated for code and data segments of compiled functions. /// /// # Safety @@ -173,29 +164,6 @@ impl SimpleJITProduct { self.memory.writable.free_memory(); } - /// Get the `FuncOrDataId` associated with the given name. - pub fn func_or_data_for_func(&self, name: &str) -> Option { - self.declarations.get_name(name) - } - - /// Return the address of a function. - pub fn lookup_func(&self, func_id: FuncId) -> *const u8 { - self.functions[func_id] - .as_ref() - .unwrap_or_else(|| panic!("{} is not defined", func_id)) - .ptr - } - - /// Return the address and size of a data object. - pub fn lookup_data(&self, data_id: DataId) -> (*const u8, usize) { - let data = self.data_objects[data_id] - .as_ref() - .unwrap_or_else(|| panic!("{} is not defined", data_id)); - (data.ptr, data.size) - } -} - -impl SimpleJITModule { fn lookup_symbol(&self, name: &str) -> Option<*const u8> { self.symbols .get(name) @@ -547,26 +515,6 @@ impl<'simple_jit_backend> Module for SimpleJITModule { } } -impl SimpleJITModule { - /// SimpleJIT emits code and data into memory as it processes them. This - /// method performs no additional processing, but returns a handle which - /// allows freeing the allocated memory. Otherwise said memory is leaked - /// to enable safe handling of the resulting pointers. - /// - /// This method does not need to be called when access to the memory - /// handle is not required. - pub fn finish(mut self) -> SimpleJITProduct { - self.finalize_definitions(); - - SimpleJITProduct { - memory: self.memory, - declarations: self.declarations, - functions: self.functions, - data_objects: self.data_objects, - } - } -} - #[cfg(not(windows))] fn lookup_with_dlsym(name: &str) -> Option<*const u8> { let c_str = CString::new(name).unwrap(); diff --git a/cranelift/simplejit/src/lib.rs b/cranelift/simplejit/src/lib.rs index 208106b93d..de4509fca0 100644 --- a/cranelift/simplejit/src/lib.rs +++ b/cranelift/simplejit/src/lib.rs @@ -26,7 +26,7 @@ mod backend; mod compiled_blob; mod memory; -pub use crate::backend::{SimpleJITBuilder, SimpleJITModule, SimpleJITProduct}; +pub use crate::backend::{SimpleJITBuilder, SimpleJITModule}; /// Version number of this crate. pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/cranelift/simplejit/tests/basic.rs b/cranelift/simplejit/tests/basic.rs index e7b52417c1..0b91765433 100644 --- a/cranelift/simplejit/tests/basic.rs +++ b/cranelift/simplejit/tests/basic.rs @@ -186,5 +186,5 @@ fn libcall_function() { .define_function(func_id, &mut ctx, &mut trap_sink) .unwrap(); - module.finish(); + module.finalize_definitions(); } From ffe9de15fc7b8ec273b55161cf995f3d8548ff07 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 9 Nov 2020 10:18:43 +0100 Subject: [PATCH 05/10] Enable unreachable_pub lint --- cranelift/simplejit/src/lib.rs | 3 ++- cranelift/simplejit/src/memory.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cranelift/simplejit/src/lib.rs b/cranelift/simplejit/src/lib.rs index de4509fca0..d558b44396 100644 --- a/cranelift/simplejit/src/lib.rs +++ b/cranelift/simplejit/src/lib.rs @@ -4,7 +4,8 @@ missing_docs, trivial_numeric_casts, unused_extern_crates, - unstable_features + unstable_features, + unreachable_pub )] #![warn(unused_import_braces)] #![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))] diff --git a/cranelift/simplejit/src/memory.rs b/cranelift/simplejit/src/memory.rs index 7d0d84311b..0b8ae5021d 100644 --- a/cranelift/simplejit/src/memory.rs +++ b/cranelift/simplejit/src/memory.rs @@ -126,7 +126,7 @@ impl Drop for PtrLen { /// accessible memory. Memory will be leaked by default to have /// function pointers remain valid for the remainder of the /// program's life. -pub struct Memory { +pub(crate) struct Memory { allocations: Vec, executable: usize, current: PtrLen, @@ -134,7 +134,7 @@ pub struct Memory { } impl Memory { - pub fn new() -> Self { + pub(crate) fn new() -> Self { Self { allocations: Vec::new(), executable: 0, @@ -150,7 +150,7 @@ impl Memory { } /// TODO: Use a proper error type. - pub fn allocate(&mut self, size: usize, align: u64) -> Result<*mut u8, String> { + pub(crate) fn allocate(&mut self, size: usize, align: u64) -> Result<*mut u8, String> { let align = usize::try_from(align).expect("alignment too big"); if self.position % align != 0 { self.position += align - self.position % align; @@ -173,7 +173,7 @@ impl Memory { } /// Set all memory allocated in this `Memory` up to now as readable and executable. - pub fn set_readable_and_executable(&mut self) { + pub(crate) fn set_readable_and_executable(&mut self) { self.finish_current(); #[cfg(feature = "selinux-fix")] @@ -202,7 +202,7 @@ impl Memory { } /// Set all memory allocated in this `Memory` up to now as readonly. - pub fn set_readonly(&mut self) { + pub(crate) fn set_readonly(&mut self) { self.finish_current(); #[cfg(feature = "selinux-fix")] @@ -232,7 +232,7 @@ impl Memory { /// Frees all allocated memory regions that would be leaked otherwise. /// Likely to invalidate existing function pointers, causing unsafety. - pub unsafe fn free_memory(&mut self) { + pub(crate) unsafe fn free_memory(&mut self) { self.allocations.clear(); } } From 79f6f72e78612f5ec59b76866f44e28584e422a7 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 9 Nov 2020 10:27:59 +0100 Subject: [PATCH 06/10] Remove StackMapSink from SimpleJIT The stack maps are discarded anyway --- cranelift/simplejit/src/backend.rs | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index 72e49bd02f..a436a3cd23 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -5,7 +5,7 @@ use cranelift_codegen::isa::TargetIsa; use cranelift_codegen::settings::Configurable; use cranelift_codegen::{self, ir, settings}; use cranelift_codegen::{ - binemit::{Addend, CodeInfo, CodeOffset, Reloc, RelocSink, StackMap, StackMapSink, TrapSink}, + binemit::{self, Addend, CodeInfo, CodeOffset, Reloc, RelocSink, TrapSink}, CodegenError, }; use cranelift_entity::SecondaryMap; @@ -135,13 +135,6 @@ pub struct SimpleJITModule { data_objects_to_finalize: Vec, } -struct StackMapRecord { - #[allow(dead_code)] - offset: CodeOffset, - #[allow(dead_code)] - stack_map: StackMap, -} - /// A handle to allow freeing memory allocated by the `Module`. struct MemoryHandle { code: Memory, @@ -371,7 +364,7 @@ impl<'simple_jit_backend> Module for SimpleJITModule { .expect("TODO: handle OOM etc."); let mut reloc_sink = SimpleJITRelocSink::default(); - let mut stack_map_sink = SimpleJITStackMapSink::default(); + let mut stack_map_sink = binemit::NullStackMapSink {}; unsafe { ctx.emit_to_memory( &*self.isa, @@ -604,14 +597,3 @@ impl RelocSink for SimpleJITRelocSink { } } } - -#[derive(Default)] -struct SimpleJITStackMapSink { - stack_maps: Vec, -} - -impl StackMapSink for SimpleJITStackMapSink { - fn add_stack_map(&mut self, offset: CodeOffset, stack_map: StackMap) { - self.stack_maps.push(StackMapRecord { offset, stack_map }); - } -} From 844a52e96acc6a02f88e4368b2e8be94f45308bb Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 9 Nov 2020 14:53:15 +0100 Subject: [PATCH 07/10] Don't unnecessarily take &self for some ModuleDeclarations methods --- cranelift/module/src/module.rs | 52 ++++++++++++++++-------------- cranelift/object/src/backend.rs | 6 ++-- cranelift/simplejit/src/backend.rs | 6 ++-- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/cranelift/module/src/module.rs b/cranelift/module/src/module.rs index 7112c1e86c..eb4e668b98 100644 --- a/cranelift/module/src/module.rs +++ b/cranelift/module/src/module.rs @@ -29,6 +29,18 @@ impl From for ir::ExternalName { } } +impl FuncId { + /// Get the `FuncId` for the function named by `name`. + pub fn from_name(name: &ir::ExternalName) -> FuncId { + if let ir::ExternalName::User { namespace, index } = *name { + debug_assert_eq!(namespace, 0); + FuncId::from_u32(index) + } else { + panic!("unexpected ExternalName kind {}", name) + } + } +} + /// A data object identifier for use in the `Module` interface. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct DataId(u32); @@ -44,6 +56,18 @@ impl From for ir::ExternalName { } } +impl DataId { + /// Get the `DataId` for the data object named by `name`. + pub fn from_name(name: &ir::ExternalName) -> DataId { + if let ir::ExternalName::User { namespace, index } = *name { + debug_assert_eq!(namespace, 1); + DataId::from_u32(index) + } else { + panic!("unexpected ExternalName kind {}", name) + } + } +} + /// Linkage refers to where an entity is defined and who can see it. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Linkage { @@ -214,21 +238,10 @@ impl ModuleDeclarations { self.functions.iter() } - /// Get the `FuncId` for the function named by `name`. - pub fn get_function_id(&self, name: &ir::ExternalName) -> FuncId { - if let ir::ExternalName::User { namespace, index } = *name { - debug_assert_eq!(namespace, 0); - FuncId::from_u32(index) - } else { - panic!("unexpected ExternalName kind {}", name) - } - } - - /// Get the `DataId` for the data object named by `name`. - pub fn get_data_id(&self, name: &ir::ExternalName) -> DataId { - if let ir::ExternalName::User { namespace, index } = *name { - debug_assert_eq!(namespace, 1); - DataId::from_u32(index) + /// Return whether `name` names a function, rather than a data object. + pub fn is_function(name: &ir::ExternalName) -> bool { + if let ir::ExternalName::User { namespace, .. } = *name { + namespace == 0 } else { panic!("unexpected ExternalName kind {}", name) } @@ -249,15 +262,6 @@ impl ModuleDeclarations { &self.data_objects[data_id] } - /// Return whether `name` names a function, rather than a data object. - pub fn is_function(&self, name: &ir::ExternalName) -> bool { - if let ir::ExternalName::User { namespace, .. } = *name { - namespace == 0 - } else { - panic!("unexpected ExternalName kind {}", name) - } - } - /// Declare a function in this module. pub fn declare_function( &mut self, diff --git a/cranelift/object/src/backend.rs b/cranelift/object/src/backend.rs index e27ebcde04..853289d3e2 100644 --- a/cranelift/object/src/backend.rs +++ b/cranelift/object/src/backend.rs @@ -477,11 +477,11 @@ impl ObjectModule { fn get_symbol(&mut self, name: &ir::ExternalName) -> SymbolId { match *name { ir::ExternalName::User { .. } => { - if self.declarations.is_function(name) { - let id = self.declarations.get_function_id(name); + if ModuleDeclarations::is_function(name) { + let id = FuncId::from_name(name); self.functions[id].unwrap().0 } else { - let id = self.declarations.get_data_id(name); + let id = DataId::from_name(name); self.data_objects[id].unwrap().0 } } diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index a436a3cd23..727fecd10a 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -167,8 +167,8 @@ impl SimpleJITModule { fn get_definition(&self, name: &ir::ExternalName) -> *const u8 { match *name { ir::ExternalName::User { .. } => { - let (name, linkage) = if self.declarations.is_function(name) { - let func_id = self.declarations.get_function_id(name); + let (name, linkage) = if ModuleDeclarations::is_function(name) { + let func_id = FuncId::from_name(name); match &self.functions[func_id] { Some(compiled) => return compiled.ptr, None => { @@ -177,7 +177,7 @@ impl SimpleJITModule { } } } else { - let data_id = self.declarations.get_data_id(name); + let data_id = DataId::from_name(name); match &self.data_objects[data_id] { Some(compiled) => return compiled.ptr, None => { From a5501e12a65dd1a732a0a4648b31128d8f32716e Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 11 Nov 2020 11:56:41 +0100 Subject: [PATCH 08/10] Rename functions and data_objects fields --- cranelift/simplejit/src/backend.rs | 35 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index 727fecd10a..febee43b2b 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -129,8 +129,8 @@ pub struct SimpleJITModule { libcall_names: Box String>, memory: MemoryHandle, declarations: ModuleDeclarations, - functions: SecondaryMap>, - data_objects: SecondaryMap>, + compiled_functions: SecondaryMap>, + compiled_data_objects: SecondaryMap>, functions_to_finalize: Vec, data_objects_to_finalize: Vec, } @@ -169,7 +169,7 @@ impl SimpleJITModule { ir::ExternalName::User { .. } => { let (name, linkage) = if ModuleDeclarations::is_function(name) { let func_id = FuncId::from_name(name); - match &self.functions[func_id] { + match &self.compiled_functions[func_id] { Some(compiled) => return compiled.ptr, None => { let decl = self.declarations.get_function_decl(func_id); @@ -178,7 +178,7 @@ impl SimpleJITModule { } } else { let data_id = DataId::from_name(name); - match &self.data_objects[data_id] { + match &self.compiled_data_objects[data_id] { Some(compiled) => return compiled.ptr, None => { let decl = self.declarations.get_data_decl(data_id); @@ -205,7 +205,7 @@ impl SimpleJITModule { /// Returns the address of a finalized function. pub fn get_finalized_function(&self, func_id: FuncId) -> *const u8 { - let info = &self.functions[func_id]; + let info = &self.compiled_functions[func_id]; debug_assert!( !self.functions_to_finalize.iter().any(|x| *x == func_id), "function not yet finalized" @@ -217,7 +217,7 @@ impl SimpleJITModule { /// Returns the address and size of a finalized data object. pub fn get_finalized_data(&self, data_id: DataId) -> (*const u8, usize) { - let info = &self.data_objects[data_id]; + let info = &self.compiled_data_objects[data_id]; debug_assert!( !self.data_objects_to_finalize.iter().any(|x| *x == data_id), "data object not yet finalized" @@ -256,7 +256,7 @@ impl SimpleJITModule { for func in std::mem::take(&mut self.functions_to_finalize) { let decl = self.declarations.get_function_decl(func); debug_assert!(decl.linkage.is_definable()); - let func = self.functions[func] + let func = self.compiled_functions[func] .as_ref() .expect("function must be compiled before it can be finalized"); func.perform_relocations(|name| self.get_definition(name)); @@ -264,7 +264,7 @@ impl SimpleJITModule { for data in std::mem::take(&mut self.data_objects_to_finalize) { let decl = self.declarations.get_data_decl(data); debug_assert!(decl.linkage.is_definable()); - let data = self.data_objects[data] + let data = self.compiled_data_objects[data] .as_ref() .expect("data object must be compiled before it can be finalized"); data.perform_relocations(|name| self.get_definition(name)); @@ -289,8 +289,8 @@ impl SimpleJITModule { libcall_names: builder.libcall_names, memory, declarations: ModuleDeclarations::default(), - functions: SecondaryMap::new(), - data_objects: SecondaryMap::new(), + compiled_functions: SecondaryMap::new(), + compiled_data_objects: SecondaryMap::new(), functions_to_finalize: Vec::new(), data_objects_to_finalize: Vec::new(), } @@ -352,7 +352,7 @@ impl<'simple_jit_backend> Module for SimpleJITModule { return Err(ModuleError::InvalidImportDefinition(decl.name.clone())); } - if !self.functions[id].is_none() { + if !self.compiled_functions[id].is_none() { return Err(ModuleError::DuplicateDefinition(decl.name.to_owned())); } @@ -376,7 +376,7 @@ impl<'simple_jit_backend> Module for SimpleJITModule { }; self.record_function_for_perf(ptr, size, &decl.name); - self.functions[id] = Some(CompiledBlob { + self.compiled_functions[id] = Some(CompiledBlob { ptr, size, relocs: reloc_sink.relocs, @@ -403,7 +403,7 @@ impl<'simple_jit_backend> Module for SimpleJITModule { return Err(ModuleError::InvalidImportDefinition(decl.name.clone())); } - if !self.functions[id].is_none() { + if !self.compiled_functions[id].is_none() { return Err(ModuleError::DuplicateDefinition(decl.name.to_owned())); } @@ -419,7 +419,7 @@ impl<'simple_jit_backend> Module for SimpleJITModule { } self.record_function_for_perf(ptr, size, &decl.name); - self.functions[id] = Some(CompiledBlob { + self.compiled_functions[id] = Some(CompiledBlob { ptr, size, relocs: relocs.to_vec(), @@ -435,14 +435,12 @@ impl<'simple_jit_backend> Module for SimpleJITModule { return Err(ModuleError::InvalidImportDefinition(decl.name.clone())); } - if !self.data_objects[id].is_none() { + if !self.compiled_data_objects[id].is_none() { return Err(ModuleError::DuplicateDefinition(decl.name.to_owned())); } assert!(!decl.tls, "SimpleJIT doesn't yet support TLS"); - self.data_objects_to_finalize.push(id); - let &DataDescription { ref init, ref function_decls, @@ -502,7 +500,8 @@ impl<'simple_jit_backend> Module for SimpleJITModule { }); } - self.data_objects[id] = Some(CompiledBlob { ptr, size, relocs }); + self.compiled_data_objects[id] = Some(CompiledBlob { ptr, size, relocs }); + self.data_objects_to_finalize.push(id); Ok(()) } From 1dc27c93a4aa09eda18b54ffbf92f5e6955dd047 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 11 Nov 2020 12:19:37 +0100 Subject: [PATCH 09/10] Introduce DataDescription::all_relocs to dedup some code --- cranelift/module/src/data_context.rs | 32 +++++- cranelift/object/src/backend.rs | 164 ++++++++++++--------------- cranelift/simplejit/src/backend.rs | 31 ++--- 3 files changed, 111 insertions(+), 116 deletions(-) diff --git a/cranelift/module/src/data_context.rs b/cranelift/module/src/data_context.rs index ca490a09e9..ce9536b1b0 100644 --- a/cranelift/module/src/data_context.rs +++ b/cranelift/module/src/data_context.rs @@ -1,6 +1,6 @@ //! Defines `DataContext`. -use cranelift_codegen::binemit::{Addend, CodeOffset}; +use cranelift_codegen::binemit::{Addend, CodeOffset, Reloc}; use cranelift_codegen::entity::PrimaryMap; use cranelift_codegen::ir; use std::borrow::ToOwned; @@ -8,6 +8,8 @@ use std::boxed::Box; use std::string::String; use std::vec::Vec; +use crate::RelocRecord; + /// This specifies how data is to be initialized. #[derive(PartialEq, Eq, Debug)] pub enum Init { @@ -55,6 +57,34 @@ pub struct DataDescription { pub align: Option, } +impl DataDescription { + /// An iterator over all relocations of the data object. + pub fn all_relocs<'a>( + &'a self, + pointer_reloc: Reloc, + ) -> impl Iterator + 'a { + let func_relocs = self + .function_relocs + .iter() + .map(move |&(offset, id)| RelocRecord { + reloc: pointer_reloc, + offset, + name: self.function_decls[id].clone(), + addend: 0, + }); + let data_relocs = self + .data_relocs + .iter() + .map(move |&(offset, id, addend)| RelocRecord { + reloc: pointer_reloc, + offset, + name: self.data_decls[id].clone(), + addend, + }); + func_relocs.chain(data_relocs) + } +} + /// This is to data objects what cranelift_codegen::Context is to functions. pub struct DataContext { description: DataDescription, diff --git a/cranelift/object/src/backend.rs b/cranelift/object/src/backend.rs index 853289d3e2..f8bffc8beb 100644 --- a/cranelift/object/src/backend.rs +++ b/cranelift/object/src/backend.rs @@ -305,7 +305,10 @@ impl Module for ObjectModule { }; if !relocs.is_empty() { - let relocs = self.process_relocs(relocs); + let relocs = relocs + .iter() + .map(|record| self.process_reloc(record)) + .collect(); self.relocs.push(SymbolRelocs { section, offset, @@ -330,40 +333,24 @@ impl Module for ObjectModule { let &DataDescription { ref init, - ref function_decls, - ref data_decls, - ref function_relocs, - ref data_relocs, + function_decls: _, + data_decls: _, + function_relocs: _, + data_relocs: _, ref custom_segment_section, align, } = data_ctx.description(); - let reloc_size = match self.isa.triple().pointer_width().unwrap() { - PointerWidth::U16 => 16, - PointerWidth::U32 => 32, - PointerWidth::U64 => 64, + let pointer_reloc = match self.isa.triple().pointer_width().unwrap() { + PointerWidth::U16 => unimplemented!("16bit pointers"), + PointerWidth::U32 => Reloc::Abs4, + PointerWidth::U64 => Reloc::Abs8, }; - let mut relocs = Vec::new(); - for &(offset, id) in function_relocs { - relocs.push(ObjectRelocRecord { - offset, - name: function_decls[id].clone(), - kind: RelocationKind::Absolute, - encoding: RelocationEncoding::Generic, - size: reloc_size, - addend: 0, - }); - } - for &(offset, id, addend) in data_relocs { - relocs.push(ObjectRelocRecord { - offset, - name: data_decls[id].clone(), - kind: RelocationKind::Absolute, - encoding: RelocationEncoding::Generic, - size: reloc_size, - addend, - }); - } + let relocs = data_ctx + .description() + .all_relocs(pointer_reloc) + .map(|record| self.process_reloc(&record)) + .collect::>(); let section = if custom_segment_section.is_none() { let section_kind = if let Init::Zeros { .. } = *init { @@ -510,69 +497,60 @@ impl ObjectModule { } } - fn process_relocs(&self, relocs: &[RelocRecord]) -> Vec { - relocs - .iter() - .map(|record| { - let mut addend = record.addend; - let (kind, encoding, size) = match record.reloc { - Reloc::Abs4 => (RelocationKind::Absolute, RelocationEncoding::Generic, 32), - Reloc::Abs8 => (RelocationKind::Absolute, RelocationEncoding::Generic, 64), - Reloc::X86PCRel4 => (RelocationKind::Relative, RelocationEncoding::Generic, 32), - Reloc::X86CallPCRel4 => { - (RelocationKind::Relative, RelocationEncoding::X86Branch, 32) - } - // TODO: Get Cranelift to tell us when we can use - // R_X86_64_GOTPCRELX/R_X86_64_REX_GOTPCRELX. - Reloc::X86CallPLTRel4 => ( - RelocationKind::PltRelative, - RelocationEncoding::X86Branch, - 32, - ), - Reloc::X86GOTPCRel4 => { - (RelocationKind::GotRelative, RelocationEncoding::Generic, 32) - } - Reloc::ElfX86_64TlsGd => { - assert_eq!( - self.object.format(), - object::BinaryFormat::Elf, - "ElfX86_64TlsGd is not supported for this file format" - ); - ( - RelocationKind::Elf(object::elf::R_X86_64_TLSGD), - RelocationEncoding::Generic, - 32, - ) - } - Reloc::MachOX86_64Tlv => { - assert_eq!( - self.object.format(), - object::BinaryFormat::MachO, - "MachOX86_64Tlv is not supported for this file format" - ); - addend += 4; // X86_64_RELOC_TLV has an implicit addend of -4 - ( - RelocationKind::MachO { - value: object::macho::X86_64_RELOC_TLV, - relative: true, - }, - RelocationEncoding::Generic, - 32, - ) - } - // FIXME - _ => unimplemented!(), - }; - ObjectRelocRecord { - offset: record.offset, - name: record.name.clone(), - kind, - encoding, - size, - addend, - } - }) - .collect() + fn process_reloc(&self, record: &RelocRecord) -> ObjectRelocRecord { + let mut addend = record.addend; + let (kind, encoding, size) = match record.reloc { + Reloc::Abs4 => (RelocationKind::Absolute, RelocationEncoding::Generic, 32), + Reloc::Abs8 => (RelocationKind::Absolute, RelocationEncoding::Generic, 64), + Reloc::X86PCRel4 => (RelocationKind::Relative, RelocationEncoding::Generic, 32), + Reloc::X86CallPCRel4 => (RelocationKind::Relative, RelocationEncoding::X86Branch, 32), + // TODO: Get Cranelift to tell us when we can use + // R_X86_64_GOTPCRELX/R_X86_64_REX_GOTPCRELX. + Reloc::X86CallPLTRel4 => ( + RelocationKind::PltRelative, + RelocationEncoding::X86Branch, + 32, + ), + Reloc::X86GOTPCRel4 => (RelocationKind::GotRelative, RelocationEncoding::Generic, 32), + Reloc::ElfX86_64TlsGd => { + assert_eq!( + self.object.format(), + object::BinaryFormat::Elf, + "ElfX86_64TlsGd is not supported for this file format" + ); + ( + RelocationKind::Elf(object::elf::R_X86_64_TLSGD), + RelocationEncoding::Generic, + 32, + ) + } + Reloc::MachOX86_64Tlv => { + assert_eq!( + self.object.format(), + object::BinaryFormat::MachO, + "MachOX86_64Tlv is not supported for this file format" + ); + addend += 4; // X86_64_RELOC_TLV has an implicit addend of -4 + ( + RelocationKind::MachO { + value: object::macho::X86_64_RELOC_TLV, + relative: true, + }, + RelocationEncoding::Generic, + 32, + ) + } + // FIXME + _ => unimplemented!(), + }; + ObjectRelocRecord { + offset: record.offset, + name: record.name.clone(), + kind, + encoding, + size, + addend, + } } } diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index febee43b2b..470b504bf0 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -443,10 +443,10 @@ impl<'simple_jit_backend> Module for SimpleJITModule { let &DataDescription { ref init, - ref function_decls, - ref data_decls, - ref function_relocs, - ref data_relocs, + function_decls: _, + data_decls: _, + function_relocs: _, + data_relocs: _, custom_segment_section: _, align, } = data.description(); @@ -477,28 +477,15 @@ impl<'simple_jit_backend> Module for SimpleJITModule { } } - let reloc = match self.isa.triple().pointer_width().unwrap() { + let pointer_reloc = match self.isa.triple().pointer_width().unwrap() { PointerWidth::U16 => panic!(), PointerWidth::U32 => Reloc::Abs4, PointerWidth::U64 => Reloc::Abs8, }; - let mut relocs = Vec::new(); - for &(offset, id) in function_relocs { - relocs.push(RelocRecord { - reloc, - offset, - name: function_decls[id].clone(), - addend: 0, - }); - } - for &(offset, id, addend) in data_relocs { - relocs.push(RelocRecord { - reloc, - offset, - name: data_decls[id].clone(), - addend, - }); - } + let relocs = data + .description() + .all_relocs(pointer_reloc) + .collect::>(); self.compiled_data_objects[id] = Some(CompiledBlob { ptr, size, relocs }); self.data_objects_to_finalize.push(id); From b7a93c2321c3eff101f22e426fae8b81bd21c39e Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 11 Nov 2020 12:25:36 +0100 Subject: [PATCH 10/10] Remove reloc_block It isn't called and all reloc sinks either ignore it or panic when it is called. --- cranelift/codegen/src/binemit/memorysink.rs | 9 --------- cranelift/codegen/src/binemit/mod.rs | 3 --- cranelift/codegen/src/isa/test_utils.rs | 2 -- cranelift/filetests/src/test_binemit.rs | 4 ---- cranelift/filetests/src/test_compile.rs | 1 - cranelift/filetests/src/test_rodata.rs | 1 - cranelift/filetests/src/test_stack_maps.rs | 1 - cranelift/object/src/backend.rs | 4 ---- cranelift/simplejit/src/backend.rs | 4 ---- cranelift/src/disasm.rs | 16 ---------------- crates/cranelift/src/lib.rs | 9 --------- crates/jit/src/trampoline.rs | 8 -------- crates/lightbeam/src/translate_sections.rs | 4 ---- crates/lightbeam/wasmtime/src/lib.rs | 9 --------- 14 files changed, 75 deletions(-) diff --git a/cranelift/codegen/src/binemit/memorysink.rs b/cranelift/codegen/src/binemit/memorysink.rs index 151983933d..d50d1c10eb 100644 --- a/cranelift/codegen/src/binemit/memorysink.rs +++ b/cranelift/codegen/src/binemit/memorysink.rs @@ -74,9 +74,6 @@ impl<'a> MemoryCodeSink<'a> { /// A trait for receiving relocations for code that is emitted directly into memory. pub trait RelocSink { - /// Add a relocation referencing a block at the current offset. - fn reloc_block(&mut self, _: CodeOffset, _: Reloc, _: CodeOffset); - /// Add a relocation referencing an external symbol at the current offset. fn reloc_external( &mut self, @@ -138,11 +135,6 @@ impl<'a> CodeSink for MemoryCodeSink<'a> { self.write(x); } - fn reloc_block(&mut self, rel: Reloc, block_offset: CodeOffset) { - let ofs = self.offset(); - self.relocs.reloc_block(ofs, rel, block_offset); - } - fn reloc_external( &mut self, srcloc: SourceLoc, @@ -204,7 +196,6 @@ impl<'a> CodeSink for MemoryCodeSink<'a> { pub struct NullRelocSink {} impl RelocSink for NullRelocSink { - fn reloc_block(&mut self, _: CodeOffset, _: Reloc, _: CodeOffset) {} fn reloc_external( &mut self, _: CodeOffset, diff --git a/cranelift/codegen/src/binemit/mod.rs b/cranelift/codegen/src/binemit/mod.rs index ae26f7f14e..b534ec9765 100644 --- a/cranelift/codegen/src/binemit/mod.rs +++ b/cranelift/codegen/src/binemit/mod.rs @@ -140,9 +140,6 @@ pub trait CodeSink { /// Add 8 bytes to the code section. fn put8(&mut self, _: u64); - /// Add a relocation referencing a block at the current offset. - fn reloc_block(&mut self, _: Reloc, _: CodeOffset); - /// Add a relocation referencing an external symbol plus the addend at the current offset. fn reloc_external(&mut self, _: SourceLoc, _: Reloc, _: &ExternalName, _: Addend); diff --git a/cranelift/codegen/src/isa/test_utils.rs b/cranelift/codegen/src/isa/test_utils.rs index 77ce94fd4b..01c500d6ca 100644 --- a/cranelift/codegen/src/isa/test_utils.rs +++ b/cranelift/codegen/src/isa/test_utils.rs @@ -59,8 +59,6 @@ impl CodeSink for TestCodeSink { } } - fn reloc_block(&mut self, _rel: Reloc, _block_offset: CodeOffset) {} - fn reloc_external( &mut self, _srcloc: SourceLoc, diff --git a/cranelift/filetests/src/test_binemit.rs b/cranelift/filetests/src/test_binemit.rs index 32d8514c92..ab25decaab 100644 --- a/cranelift/filetests/src/test_binemit.rs +++ b/cranelift/filetests/src/test_binemit.rs @@ -72,10 +72,6 @@ impl binemit::CodeSink for TextSink { self.offset += 8; } - fn reloc_block(&mut self, reloc: binemit::Reloc, block_offset: binemit::CodeOffset) { - write!(self.text, "{}({}) ", reloc, block_offset).unwrap(); - } - fn reloc_external( &mut self, _srcloc: ir::SourceLoc, diff --git a/cranelift/filetests/src/test_compile.rs b/cranelift/filetests/src/test_compile.rs index 2a3b5da6b7..b66f8eca66 100644 --- a/cranelift/filetests/src/test_compile.rs +++ b/cranelift/filetests/src/test_compile.rs @@ -109,7 +109,6 @@ impl binemit::CodeSink for SizeSink { self.offset += 8; } - fn reloc_block(&mut self, _reloc: binemit::Reloc, _block_offset: binemit::CodeOffset) {} fn reloc_external( &mut self, _srcloc: ir::SourceLoc, diff --git a/cranelift/filetests/src/test_rodata.rs b/cranelift/filetests/src/test_rodata.rs index 6a1976f4fe..83b10b4e08 100644 --- a/cranelift/filetests/src/test_rodata.rs +++ b/cranelift/filetests/src/test_rodata.rs @@ -106,7 +106,6 @@ impl binemit::CodeSink for RodataSink { } } - fn reloc_block(&mut self, _reloc: binemit::Reloc, _block_offset: binemit::CodeOffset) {} fn reloc_external( &mut self, _: ir::SourceLoc, diff --git a/cranelift/filetests/src/test_stack_maps.rs b/cranelift/filetests/src/test_stack_maps.rs index 2b70a111d2..d9db686d02 100644 --- a/cranelift/filetests/src/test_stack_maps.rs +++ b/cranelift/filetests/src/test_stack_maps.rs @@ -79,7 +79,6 @@ impl CodeSink for TestStackMapsSink { self.offset += 8; } - fn reloc_block(&mut self, _: Reloc, _: CodeOffset) {} fn reloc_external(&mut self, _: SourceLoc, _: Reloc, _: &ExternalName, _: Addend) {} fn reloc_constant(&mut self, _: Reloc, _: ConstantOffset) {} fn reloc_jt(&mut self, _: Reloc, _: JumpTable) {} diff --git a/cranelift/object/src/backend.rs b/cranelift/object/src/backend.rs index f8bffc8beb..81dd75c4cd 100644 --- a/cranelift/object/src/backend.rs +++ b/cranelift/object/src/backend.rs @@ -622,10 +622,6 @@ struct ObjectRelocSink { } impl RelocSink for ObjectRelocSink { - fn reloc_block(&mut self, _offset: CodeOffset, _reloc: Reloc, _block_offset: CodeOffset) { - unimplemented!(); - } - fn reloc_external( &mut self, offset: CodeOffset, diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index 470b504bf0..d211eb5feb 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -539,10 +539,6 @@ struct SimpleJITRelocSink { } impl RelocSink for SimpleJITRelocSink { - fn reloc_block(&mut self, _offset: CodeOffset, _reloc: Reloc, _block_offset: CodeOffset) { - unimplemented!(); - } - fn reloc_external( &mut self, offset: CodeOffset, diff --git a/cranelift/src/disasm.rs b/cranelift/src/disasm.rs index 90dec397a5..a98e867380 100644 --- a/cranelift/src/disasm.rs +++ b/cranelift/src/disasm.rs @@ -19,22 +19,6 @@ impl PrintRelocs { } impl binemit::RelocSink for PrintRelocs { - fn reloc_block( - &mut self, - where_: binemit::CodeOffset, - r: binemit::Reloc, - offset: binemit::CodeOffset, - ) { - if self.flag_print { - writeln!( - &mut self.text, - "reloc_block: {} {} at {}", - r, offset, where_ - ) - .unwrap(); - } - } - fn reloc_external( &mut self, where_: binemit::CodeOffset, diff --git a/crates/cranelift/src/lib.rs b/crates/cranelift/src/lib.rs index f128ab5717..a0e241572e 100644 --- a/crates/cranelift/src/lib.rs +++ b/crates/cranelift/src/lib.rs @@ -114,15 +114,6 @@ struct RelocSink { } impl binemit::RelocSink for RelocSink { - fn reloc_block( - &mut self, - _offset: binemit::CodeOffset, - _reloc: binemit::Reloc, - _block_offset: binemit::CodeOffset, - ) { - // This should use the `offsets` field of `ir::Function`. - panic!("block headers not yet implemented"); - } fn reloc_external( &mut self, offset: binemit::CodeOffset, diff --git a/crates/jit/src/trampoline.rs b/crates/jit/src/trampoline.rs index 9116666ea2..aebc4f1bc8 100644 --- a/crates/jit/src/trampoline.rs +++ b/crates/jit/src/trampoline.rs @@ -184,14 +184,6 @@ impl TrampolineRelocSink { } impl binemit::RelocSink for TrampolineRelocSink { - fn reloc_block( - &mut self, - _offset: binemit::CodeOffset, - _reloc: binemit::Reloc, - _block_offset: binemit::CodeOffset, - ) { - panic!("trampoline compilation should not produce block relocs"); - } fn reloc_external( &mut self, offset: binemit::CodeOffset, diff --git a/crates/lightbeam/src/translate_sections.rs b/crates/lightbeam/src/translate_sections.rs index ad32936420..7ddad9197d 100644 --- a/crates/lightbeam/src/translate_sections.rs +++ b/crates/lightbeam/src/translate_sections.rs @@ -83,10 +83,6 @@ pub fn element(elements: ElementSectionReader) -> Result<(), Error> { struct UnimplementedRelocSink; impl binemit::RelocSink for UnimplementedRelocSink { - fn reloc_block(&mut self, _: binemit::CodeOffset, _: binemit::Reloc, _: binemit::CodeOffset) { - unimplemented!() - } - fn reloc_external( &mut self, _: binemit::CodeOffset, diff --git a/crates/lightbeam/wasmtime/src/lib.rs b/crates/lightbeam/wasmtime/src/lib.rs index a1e67c6271..ed09e04550 100644 --- a/crates/lightbeam/wasmtime/src/lib.rs +++ b/crates/lightbeam/wasmtime/src/lib.rs @@ -86,15 +86,6 @@ struct RelocSink { } impl binemit::RelocSink for RelocSink { - fn reloc_block( - &mut self, - _offset: binemit::CodeOffset, - _reloc: binemit::Reloc, - _block_offset: binemit::CodeOffset, - ) { - // This should use the `offsets` field of `ir::Function`. - panic!("block headers not yet implemented"); - } fn reloc_external( &mut self, offset: binemit::CodeOffset,