Skip to content

Commit

Permalink
Emit unwind info for main and alloc shim
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Jun 12, 2020
1 parent e1a77a5 commit ba7cdf2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
15 changes: 12 additions & 3 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,30 @@ 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)
});
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 {
Expand Down Expand Up @@ -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());
}
}
14 changes: 8 additions & 6 deletions src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/driver/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
10 changes: 8 additions & 2 deletions src/main_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
) {
Expand Down Expand Up @@ -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());
}
}

0 comments on commit ba7cdf2

Please sign in to comment.