2020-09-23 15:13:49 +02:00
|
|
|
//! Unwind info generation (`.eh_frame`)
|
|
|
|
|
2020-04-25 18:23:31 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2020-08-28 12:10:48 +02:00
|
|
|
use cranelift_codegen::isa::{unwind::UnwindInfo, TargetIsa};
|
2020-04-25 18:23:31 +02:00
|
|
|
|
2021-09-19 13:56:58 +02:00
|
|
|
use cranelift_object::ObjectProduct;
|
2020-05-01 19:21:29 +02:00
|
|
|
use gimli::write::{Address, CieId, EhFrame, FrameTable, Section};
|
2021-04-30 14:49:58 +02:00
|
|
|
use gimli::RunTimeEndian;
|
2020-04-25 18:23:31 +02:00
|
|
|
|
2021-09-19 13:56:58 +02:00
|
|
|
use super::object::WriteDebugInfo;
|
2020-05-01 19:21:29 +02:00
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
pub(crate) struct UnwindContext {
|
|
|
|
endian: RunTimeEndian,
|
2020-05-01 19:21:29 +02:00
|
|
|
frame_table: FrameTable,
|
2020-05-05 12:16:28 +02:00
|
|
|
cie_id: Option<CieId>,
|
2020-05-01 19:21:29 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
impl UnwindContext {
|
|
|
|
pub(crate) fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa, pic_eh_frame: bool) -> Self {
|
|
|
|
let endian = super::target_endian(tcx);
|
2020-05-01 19:21:29 +02:00
|
|
|
let mut frame_table = FrameTable::default();
|
|
|
|
|
2020-09-23 10:00:09 +02:00
|
|
|
let cie_id = if let Some(mut cie) = isa.create_systemv_cie() {
|
2020-12-27 10:30:38 +01:00
|
|
|
if pic_eh_frame {
|
2020-10-09 19:17:52 +02:00
|
|
|
cie.fde_address_encoding =
|
|
|
|
gimli::DwEhPe(gimli::DW_EH_PE_pcrel.0 | gimli::DW_EH_PE_sdata4.0);
|
2020-09-23 10:00:09 +02:00
|
|
|
}
|
2020-05-05 12:16:28 +02:00
|
|
|
Some(frame_table.add_cie(cie))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-05-01 19:21:29 +02:00
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
UnwindContext { endian, frame_table, cie_id }
|
2020-05-01 19:21:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn add_function(&mut self, func_id: FuncId, context: &Context, isa: &dyn TargetIsa) {
|
2020-04-25 18:23:31 +02:00
|
|
|
let unwind_info = if let Some(unwind_info) = context.create_unwind_info(isa).unwrap() {
|
|
|
|
unwind_info
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
match unwind_info {
|
|
|
|
UnwindInfo::SystemV(unwind_info) => {
|
2020-08-28 12:10:48 +02:00
|
|
|
self.frame_table.add_fde(
|
|
|
|
self.cie_id.unwrap(),
|
2021-03-05 19:12:59 +01:00
|
|
|
unwind_info
|
|
|
|
.to_fde(Address::Symbol { symbol: func_id.as_u32() as usize, addend: 0 }),
|
2020-08-28 12:10:48 +02:00
|
|
|
);
|
|
|
|
}
|
2020-04-25 18:23:31 +02:00
|
|
|
UnwindInfo::WindowsX64(_) => {
|
|
|
|
// FIXME implement this
|
|
|
|
}
|
2020-11-03 11:00:04 +01:00
|
|
|
unwind_info => unimplemented!("{:?}", unwind_info),
|
2020-04-25 18:23:31 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 19:21:29 +02:00
|
|
|
|
2021-09-19 13:56:58 +02:00
|
|
|
pub(crate) fn emit(self, product: &mut ObjectProduct) {
|
2021-04-30 14:49:58 +02:00
|
|
|
let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
|
2020-05-01 19:21:29 +02:00
|
|
|
self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
|
|
|
|
|
|
|
|
if !eh_frame.0.writer.slice().is_empty() {
|
|
|
|
let id = eh_frame.id();
|
|
|
|
let section_id = product.add_debug_section(id, eh_frame.0.writer.into_vec());
|
|
|
|
let mut section_map = FxHashMap::default();
|
|
|
|
section_map.insert(id, section_id);
|
|
|
|
|
|
|
|
for reloc in &eh_frame.0.relocs {
|
|
|
|
product.add_debug_reloc(§ion_map, §ion_id, reloc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 20:57:51 +02:00
|
|
|
|
2021-04-30 18:46:59 +02:00
|
|
|
#[cfg(all(feature = "jit", windows))]
|
|
|
|
pub(crate) unsafe fn register_jit(self, _jit_module: &cranelift_jit::JITModule) {}
|
|
|
|
|
|
|
|
#[cfg(all(feature = "jit", not(windows)))]
|
2021-04-30 14:49:58 +02:00
|
|
|
pub(crate) unsafe fn register_jit(self, jit_module: &cranelift_jit::JITModule) {
|
|
|
|
let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
|
2020-05-01 20:57:51 +02:00
|
|
|
self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
|
|
|
|
|
|
|
|
if eh_frame.0.writer.slice().is_empty() {
|
2021-04-30 14:49:58 +02:00
|
|
|
return;
|
2020-05-01 20:57:51 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 20:48:53 +01:00
|
|
|
let mut eh_frame = eh_frame.0.relocate_for_jit(jit_module);
|
2020-05-01 20:57:51 +02:00
|
|
|
|
|
|
|
// GCC expects a terminating "empty" length, so write a 0 length at the end of the table.
|
|
|
|
eh_frame.extend(&[0, 0, 0, 0]);
|
|
|
|
|
2021-04-30 14:49:58 +02:00
|
|
|
// FIXME support unregistering unwind tables once cranelift-jit supports deallocating
|
|
|
|
// individual functions
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
let (eh_frame, eh_frame_len, _) = Vec::into_raw_parts(eh_frame);
|
2020-05-01 20:57:51 +02:00
|
|
|
|
|
|
|
// =======================================================================
|
|
|
|
// Everything after this line up to the end of the file is loosly based on
|
|
|
|
// https://github.com/bytecodealliance/wasmtime/blob/4471a82b0c540ff48960eca6757ccce5b1b5c3e4/crates/jit/src/unwind/systemv.rs
|
2020-09-16 16:57:52 +02:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
{
|
|
|
|
// On macOS, `__register_frame` takes a pointer to a single FDE
|
2021-04-30 14:49:58 +02:00
|
|
|
let start = eh_frame;
|
|
|
|
let end = start.add(eh_frame_len);
|
2020-09-16 16:57:52 +02:00
|
|
|
let mut current = start;
|
|
|
|
|
|
|
|
// Walk all of the entries in the frame table and register them
|
|
|
|
while current < end {
|
|
|
|
let len = std::ptr::read::<u32>(current as *const u32) as usize;
|
|
|
|
|
|
|
|
// Skip over the CIE
|
|
|
|
if current != start {
|
|
|
|
__register_frame(current);
|
2020-05-01 20:57:51 +02:00
|
|
|
}
|
2020-09-16 16:57:52 +02:00
|
|
|
|
|
|
|
// Move to the next table entry (+4 because the length itself is not inclusive)
|
|
|
|
current = current.add(len + 4);
|
2020-05-01 20:57:51 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 16:57:52 +02:00
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
{
|
|
|
|
// On other platforms, `__register_frame` will walk the FDEs until an entry of length 0
|
2021-04-30 14:49:58 +02:00
|
|
|
__register_frame(eh_frame);
|
2020-09-16 16:57:52 +02:00
|
|
|
}
|
2020-05-01 20:57:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
// libunwind import
|
|
|
|
fn __register_frame(fde: *const u8);
|
2020-04-25 18:23:31 +02:00
|
|
|
}
|