Skip to content

Commit ba7cdf2

Browse files
committed
Emit unwind info for main and alloc shim
Fixes rust-lang#988
1 parent e1a77a5 commit ba7cdf2

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

src/allocator.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,30 @@ use crate::prelude::*;
1313
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
1414

1515
/// Returns whether an allocator shim was created
16-
pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut Module<impl Backend + 'static>) -> bool {
16+
pub(crate) fn codegen(
17+
tcx: TyCtxt<'_>,
18+
module: &mut Module<impl Backend + 'static>,
19+
unwind_context: &mut UnwindContext<'_>,
20+
) -> bool {
1721
let any_dynamic_crate = tcx.dependency_formats(LOCAL_CRATE).iter().any(|(_, list)| {
1822
use rustc_middle::middle::dependency_format::Linkage;
1923
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
2024
});
2125
if any_dynamic_crate {
2226
false
2327
} else if let Some(kind) = tcx.allocator_kind() {
24-
codegen_inner(module, kind);
28+
codegen_inner(module, unwind_context, kind);
2529
true
2630
} else {
2731
false
2832
}
2933
}
3034

31-
fn codegen_inner(module: &mut Module<impl Backend + 'static>, kind: AllocatorKind) {
35+
fn codegen_inner(
36+
module: &mut Module<impl Backend + 'static>,
37+
unwind_context: &mut UnwindContext<'_>,
38+
kind: AllocatorKind,
39+
) {
3240
let usize_ty = module.target_config().pointer_type();
3341

3442
for method in ALLOCATOR_METHODS {
@@ -99,5 +107,6 @@ fn codegen_inner(module: &mut Module<impl Backend + 'static>, kind: AllocatorKin
99107
&mut ctx,
100108
&mut cranelift_codegen::binemit::NullTrapSink {},
101109
).unwrap();
110+
unwind_context.add_function(func_id, &ctx, module.isa());
102111
}
103112
}

src/driver/aot.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege
123123
let mut unwind_context = UnwindContext::new(tcx, &mut module);
124124

125125
super::codegen_mono_items(tcx, &mut module, debug.as_mut(), &mut unwind_context, mono_items);
126-
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module);
126+
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context);
127127

128128
emit_module(
129129
tcx,
@@ -185,19 +185,21 @@ pub(super) fn run_aot(
185185
tcx.sess.abort_if_errors();
186186

187187
let mut allocator_module = new_module(tcx, "allocator_shim".to_string());
188-
let created_alloc_shim = crate::allocator::codegen(tcx, &mut allocator_module);
188+
let mut allocator_unwind_context = UnwindContext::new(tcx, &mut allocator_module);
189+
let created_alloc_shim = crate::allocator::codegen(
190+
tcx,
191+
&mut allocator_module,
192+
&mut allocator_unwind_context,
193+
);
189194

190195
let allocator_module = if created_alloc_shim {
191-
// FIXME create .eh_frame for allocator shim
192-
let unwind_context = UnwindContext::new(tcx, &mut allocator_module);
193-
194196
let ModuleCodegenResult(module, work_product) = emit_module(
195197
tcx,
196198
"allocator_shim".to_string(),
197199
ModuleKind::Allocator,
198200
allocator_module,
199201
None,
200-
unwind_context,
202+
allocator_unwind_context,
201203
);
202204
if let Some((id, product)) = work_product {
203205
work_products.insert(id, product);

src/driver/jit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>) -> ! {
5757
super::time(tcx, "codegen mono items", || {
5858
super::codegen_mono_items(tcx, &mut jit_module, None, &mut unwind_context, mono_items);
5959
});
60-
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module);
61-
crate::allocator::codegen(tcx, &mut jit_module);
60+
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, &mut unwind_context);
61+
crate::allocator::codegen(tcx, &mut jit_module, &mut unwind_context);
6262

6363
jit_module.finalize_definitions();
6464

src/main_shim.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ use crate::prelude::*;
22

33
/// Create the `main` function which will initialize the rust runtime and call
44
/// users main function.
5-
pub(crate) fn maybe_create_entry_wrapper(tcx: TyCtxt<'_>, module: &mut Module<impl Backend + 'static>) {
5+
pub(crate) fn maybe_create_entry_wrapper(
6+
tcx: TyCtxt<'_>,
7+
module: &mut Module<impl Backend + 'static>,
8+
unwind_context: &mut UnwindContext<'_>,
9+
) {
610
use rustc_hir::lang_items::StartFnLangItem;
711
use rustc_session::config::EntryFnType;
812

@@ -22,11 +26,12 @@ pub(crate) fn maybe_create_entry_wrapper(tcx: TyCtxt<'_>, module: &mut Module<im
2226
return;
2327
}
2428

25-
create_entry_fn(tcx, module, main_def_id, use_start_lang_item);
29+
create_entry_fn(tcx, module, unwind_context, main_def_id, use_start_lang_item);
2630

2731
fn create_entry_fn(
2832
tcx: TyCtxt<'_>,
2933
m: &mut Module<impl Backend + 'static>,
34+
unwind_context: &mut UnwindContext<'_>,
3035
rust_main_def_id: DefId,
3136
use_start_lang_item: bool,
3237
) {
@@ -109,5 +114,6 @@ pub(crate) fn maybe_create_entry_wrapper(tcx: TyCtxt<'_>, module: &mut Module<im
109114
&mut ctx,
110115
&mut cranelift_codegen::binemit::NullTrapSink {},
111116
).unwrap();
117+
unwind_context.add_function(cmain_func_id, &ctx, m.isa());
112118
}
113119
}

0 commit comments

Comments
 (0)