Sync rustc_codegen_cranelift 'ddd4ce25535cf71203ba3700896131ce55fde795'

This commit is contained in:
Erin Power
2021-04-30 14:49:58 +02:00
43 changed files with 784 additions and 1243 deletions

View File

@@ -5,17 +5,19 @@ use crate::prelude::*;
use cranelift_codegen::isa::{unwind::UnwindInfo, TargetIsa};
use gimli::write::{Address, CieId, EhFrame, FrameTable, Section};
use gimli::RunTimeEndian;
use crate::backend::WriteDebugInfo;
pub(crate) struct UnwindContext<'tcx> {
tcx: TyCtxt<'tcx>,
pub(crate) struct UnwindContext {
endian: RunTimeEndian,
frame_table: FrameTable,
cie_id: Option<CieId>,
}
impl<'tcx> UnwindContext<'tcx> {
pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa, pic_eh_frame: bool) -> Self {
impl UnwindContext {
pub(crate) fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa, pic_eh_frame: bool) -> Self {
let endian = super::target_endian(tcx);
let mut frame_table = FrameTable::default();
let cie_id = if let Some(mut cie) = isa.create_systemv_cie() {
@@ -28,7 +30,7 @@ impl<'tcx> UnwindContext<'tcx> {
None
};
UnwindContext { tcx, frame_table, cie_id }
UnwindContext { endian, frame_table, cie_id }
}
pub(crate) fn add_function(&mut self, func_id: FuncId, context: &Context, isa: &dyn TargetIsa) {
@@ -54,8 +56,7 @@ impl<'tcx> UnwindContext<'tcx> {
}
pub(crate) fn emit<P: WriteDebugInfo>(self, product: &mut P) {
let mut eh_frame =
EhFrame::from(super::emit::WriterRelocate::new(super::target_endian(self.tcx)));
let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
if !eh_frame.0.writer.slice().is_empty() {
@@ -71,16 +72,12 @@ impl<'tcx> UnwindContext<'tcx> {
}
#[cfg(feature = "jit")]
pub(crate) unsafe fn register_jit(
self,
jit_module: &cranelift_jit::JITModule,
) -> Option<UnwindRegistry> {
let mut eh_frame =
EhFrame::from(super::emit::WriterRelocate::new(super::target_endian(self.tcx)));
pub(crate) unsafe fn register_jit(self, jit_module: &cranelift_jit::JITModule) {
let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
if eh_frame.0.writer.slice().is_empty() {
return None;
return;
}
let mut eh_frame = eh_frame.0.relocate_for_jit(jit_module);
@@ -88,7 +85,10 @@ impl<'tcx> UnwindContext<'tcx> {
// GCC expects a terminating "empty" length, so write a 0 length at the end of the table.
eh_frame.extend(&[0, 0, 0, 0]);
let mut registrations = Vec::new();
// 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);
// =======================================================================
// Everything after this line up to the end of the file is loosly based on
@@ -96,8 +96,8 @@ impl<'tcx> UnwindContext<'tcx> {
#[cfg(target_os = "macos")]
{
// On macOS, `__register_frame` takes a pointer to a single FDE
let start = eh_frame.as_ptr();
let end = start.add(eh_frame.len());
let start = eh_frame;
let end = start.add(eh_frame_len);
let mut current = start;
// Walk all of the entries in the frame table and register them
@@ -107,7 +107,6 @@ impl<'tcx> UnwindContext<'tcx> {
// Skip over the CIE
if current != start {
__register_frame(current);
registrations.push(current as usize);
}
// Move to the next table entry (+4 because the length itself is not inclusive)
@@ -117,41 +116,12 @@ impl<'tcx> UnwindContext<'tcx> {
#[cfg(not(target_os = "macos"))]
{
// On other platforms, `__register_frame` will walk the FDEs until an entry of length 0
let ptr = eh_frame.as_ptr();
__register_frame(ptr);
registrations.push(ptr as usize);
__register_frame(eh_frame);
}
Some(UnwindRegistry { _frame_table: eh_frame, registrations })
}
}
/// Represents a registry of function unwind information for System V ABI.
pub(crate) struct UnwindRegistry {
_frame_table: Vec<u8>,
registrations: Vec<usize>,
}
extern "C" {
// libunwind import
fn __register_frame(fde: *const u8);
fn __deregister_frame(fde: *const u8);
}
impl Drop for UnwindRegistry {
fn drop(&mut self) {
unsafe {
// libgcc stores the frame entries as a linked list in decreasing sort order
// based on the PC value of the registered entry.
//
// As we store the registrations in increasing order, it would be O(N^2) to
// deregister in that order.
//
// To ensure that we just pop off the first element in the list upon every
// deregistration, walk our list of registrations backwards.
for fde in self.registrations.iter().rev() {
__deregister_frame(*fde as *const _);
}
}
}
}