Emit unwind info for main and alloc shim

Fixes #988
This commit is contained in:
bjorn3 2020-06-12 19:31:35 +02:00
parent e1a77a5ce6
commit ba7cdf21be
4 changed files with 30 additions and 13 deletions

View File

@ -13,7 +13,11 @@ use crate::prelude::*;
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
/// Returns whether an allocator shim was created
pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut Module<impl Backend + 'static>) -> bool {
pub(crate) fn codegen(
tcx: TyCtxt<'_>,
module: &mut Module<impl Backend + 'static>,
unwind_context: &mut UnwindContext<'_>,
) -> bool {
let any_dynamic_crate = tcx.dependency_formats(LOCAL_CRATE).iter().any(|(_, list)| {
use rustc_middle::middle::dependency_format::Linkage;
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
@ -21,14 +25,18 @@ pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut Module<impl Backend + 'stati
if any_dynamic_crate {
false
} else if let Some(kind) = tcx.allocator_kind() {
codegen_inner(module, kind);
codegen_inner(module, unwind_context, kind);
true
} else {
false
}
}
fn codegen_inner(module: &mut Module<impl Backend + 'static>, kind: AllocatorKind) {
fn codegen_inner(
module: &mut Module<impl Backend + 'static>,
unwind_context: &mut UnwindContext<'_>,
kind: AllocatorKind,
) {
let usize_ty = module.target_config().pointer_type();
for method in ALLOCATOR_METHODS {
@ -99,5 +107,6 @@ fn codegen_inner(module: &mut Module<impl Backend + 'static>, kind: AllocatorKin
&mut ctx,
&mut cranelift_codegen::binemit::NullTrapSink {},
).unwrap();
unwind_context.add_function(func_id, &ctx, module.isa());
}
}

View File

@ -123,7 +123,7 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege
let mut unwind_context = UnwindContext::new(tcx, &mut module);
super::codegen_mono_items(tcx, &mut module, debug.as_mut(), &mut unwind_context, mono_items);
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module);
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context);
emit_module(
tcx,
@ -185,19 +185,21 @@ pub(super) fn run_aot(
tcx.sess.abort_if_errors();
let mut allocator_module = new_module(tcx, "allocator_shim".to_string());
let created_alloc_shim = crate::allocator::codegen(tcx, &mut allocator_module);
let mut allocator_unwind_context = UnwindContext::new(tcx, &mut allocator_module);
let created_alloc_shim = crate::allocator::codegen(
tcx,
&mut allocator_module,
&mut allocator_unwind_context,
);
let allocator_module = if created_alloc_shim {
// FIXME create .eh_frame for allocator shim
let unwind_context = UnwindContext::new(tcx, &mut allocator_module);
let ModuleCodegenResult(module, work_product) = emit_module(
tcx,
"allocator_shim".to_string(),
ModuleKind::Allocator,
allocator_module,
None,
unwind_context,
allocator_unwind_context,
);
if let Some((id, product)) = work_product {
work_products.insert(id, product);

View File

@ -57,8 +57,8 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>) -> ! {
super::time(tcx, "codegen mono items", || {
super::codegen_mono_items(tcx, &mut jit_module, None, &mut unwind_context, mono_items);
});
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module);
crate::allocator::codegen(tcx, &mut jit_module);
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, &mut unwind_context);
crate::allocator::codegen(tcx, &mut jit_module, &mut unwind_context);
jit_module.finalize_definitions();

View File

@ -2,7 +2,11 @@ use crate::prelude::*;
/// Create the `main` function which will initialize the rust runtime and call
/// users main function.
pub(crate) fn maybe_create_entry_wrapper(tcx: TyCtxt<'_>, module: &mut Module<impl Backend + 'static>) {
pub(crate) fn maybe_create_entry_wrapper(
tcx: TyCtxt<'_>,
module: &mut Module<impl Backend + 'static>,
unwind_context: &mut UnwindContext<'_>,
) {
use rustc_hir::lang_items::StartFnLangItem;
use rustc_session::config::EntryFnType;
@ -22,11 +26,12 @@ pub(crate) fn maybe_create_entry_wrapper(tcx: TyCtxt<'_>, module: &mut Module<im
return;
}
create_entry_fn(tcx, module, main_def_id, use_start_lang_item);
create_entry_fn(tcx, module, unwind_context, main_def_id, use_start_lang_item);
fn create_entry_fn(
tcx: TyCtxt<'_>,
m: &mut Module<impl Backend + 'static>,
unwind_context: &mut UnwindContext<'_>,
rust_main_def_id: DefId,
use_start_lang_item: bool,
) {
@ -109,5 +114,6 @@ pub(crate) fn maybe_create_entry_wrapper(tcx: TyCtxt<'_>, module: &mut Module<im
&mut ctx,
&mut cranelift_codegen::binemit::NullTrapSink {},
).unwrap();
unwind_context.add_function(cmain_func_id, &ctx, m.isa());
}
}