Pass Module to UnwindContext

Once writing the LSDA, it will need access to the Module to get a
reference to the personality function and to define a data object for
the LSDA.

Part of rust-lang/rustc_codegen_cranelift#1567
This commit is contained in:
bjorn3 2025-04-09 14:05:27 +00:00
parent ab514c9596
commit 9495eb517e
2 changed files with 17 additions and 11 deletions

View file

@ -1,7 +1,6 @@
//! Unwind info generation (`.eh_frame`)
use cranelift_codegen::ir::Endianness;
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::isa::unwind::UnwindInfo;
use cranelift_object::ObjectProduct;
use gimli::RunTimeEndian;
@ -18,14 +17,14 @@ pub(crate) struct UnwindContext {
}
impl UnwindContext {
pub(crate) fn new(isa: &dyn TargetIsa, pic_eh_frame: bool) -> Self {
let endian = match isa.endianness() {
pub(crate) fn new(module: &mut dyn Module, pic_eh_frame: bool) -> Self {
let endian = match module.isa().endianness() {
Endianness::Little => RunTimeEndian::Little,
Endianness::Big => RunTimeEndian::Big,
};
let mut frame_table = FrameTable::default();
let cie_id = if let Some(mut cie) = isa.create_systemv_cie() {
let cie_id = if let Some(mut cie) = module.isa().create_systemv_cie() {
if pic_eh_frame {
cie.fde_address_encoding =
gimli::DwEhPe(gimli::DW_EH_PE_pcrel.0 | gimli::DW_EH_PE_sdata4.0);
@ -38,8 +37,15 @@ impl UnwindContext {
UnwindContext { endian, frame_table, cie_id }
}
pub(crate) fn add_function(&mut self, func_id: FuncId, context: &Context, isa: &dyn TargetIsa) {
if let target_lexicon::OperatingSystem::MacOSX { .. } = isa.triple().operating_system {
pub(crate) fn add_function(
&mut self,
module: &mut dyn Module,
func_id: FuncId,
context: &Context,
) {
if let target_lexicon::OperatingSystem::MacOSX { .. } =
module.isa().triple().operating_system
{
// The object crate doesn't currently support DW_GNU_EH_PE_absptr, which macOS
// requires for unwinding tables. In addition on arm64 it currently doesn't
// support 32bit relocations as we currently use for the unwinding table.
@ -48,7 +54,7 @@ impl UnwindContext {
}
let unwind_info = if let Some(unwind_info) =
context.compiled_code().unwrap().create_unwind_info(isa).unwrap()
context.compiled_code().unwrap().create_unwind_info(module.isa()).unwrap()
{
unwind_info
} else {

View file

@ -17,8 +17,8 @@ pub(crate) struct UnwindModule<T> {
}
impl<T: Module> UnwindModule<T> {
pub(crate) fn new(module: T, pic_eh_frame: bool) -> Self {
let unwind_context = UnwindContext::new(module.isa(), pic_eh_frame);
pub(crate) fn new(mut module: T, pic_eh_frame: bool) -> Self {
let unwind_context = UnwindContext::new(&mut module, pic_eh_frame);
UnwindModule { module, unwind_context }
}
}
@ -37,7 +37,7 @@ impl UnwindModule<cranelift_jit::JITModule> {
self.module.finalize_definitions().unwrap();
let prev_unwind_context = std::mem::replace(
&mut self.unwind_context,
UnwindContext::new(self.module.isa(), false),
UnwindContext::new(&mut self.module, false),
);
unsafe { prev_unwind_context.register_jit(&self.module) };
}
@ -94,7 +94,7 @@ impl<T: Module> Module for UnwindModule<T> {
ctrl_plane: &mut ControlPlane,
) -> ModuleResult<()> {
self.module.define_function_with_control_plane(func, ctx, ctrl_plane)?;
self.unwind_context.add_function(func, ctx, self.module.isa());
self.unwind_context.add_function(&mut self.module, func, ctx);
Ok(())
}