Skip to content

Commit f55c2c0

Browse files
authored
Merge pull request #1575 from rust-lang/exception-handling-preparation
Preparations for exception handling support
2 parents 0103c58 + b59f697 commit f55c2c0

File tree

5 files changed

+44
-23
lines changed

5 files changed

+44
-23
lines changed

src/base.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
566566
);
567567
}
568568
TerminatorKind::UnwindTerminate(reason) => {
569-
codegen_unwind_terminate(fx, source_info, *reason);
569+
codegen_unwind_terminate(fx, Some(source_info.span), *reason);
570570
}
571571
TerminatorKind::UnwindResume => {
572572
// FIXME implement unwinding
@@ -1117,18 +1117,10 @@ pub(crate) fn codegen_panic_nounwind<'tcx>(
11171117

11181118
pub(crate) fn codegen_unwind_terminate<'tcx>(
11191119
fx: &mut FunctionCx<'_, '_, 'tcx>,
1120-
source_info: mir::SourceInfo,
1120+
span: Option<Span>,
11211121
reason: UnwindTerminateReason,
11221122
) {
1123-
let args = [];
1124-
1125-
codegen_panic_inner(
1126-
fx,
1127-
reason.lang_item(),
1128-
&args,
1129-
UnwindAction::Terminate(UnwindTerminateReason::Abi),
1130-
Some(source_info.span),
1131-
);
1123+
codegen_panic_inner(fx, reason.lang_item(), &[], UnwindAction::Unreachable, span);
11321124
}
11331125

11341126
fn codegen_panic_inner<'tcx>(

src/compiler_builtins.rs

-1
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,4 @@ builtin_functions! {
6767
fn malloc(size: size_t) -> *mut c_void;
6868
fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
6969
fn free(p: *mut c_void) -> ();
70-
7170
}

src/debuginfo/emit.rs

+36-3
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,36 @@ impl WriterRelocate {
8181
/// Perform the collected relocations to be usable for JIT usage.
8282
#[cfg(all(feature = "jit", not(windows)))]
8383
pub(super) fn relocate_for_jit(mut self, jit_module: &cranelift_jit::JITModule) -> Vec<u8> {
84+
use cranelift_module::Module;
85+
8486
for reloc in self.relocs.drain(..) {
8587
match reloc.name {
8688
super::DebugRelocName::Section(_) => unreachable!(),
8789
super::DebugRelocName::Symbol(sym) => {
88-
let addr = jit_module.get_finalized_function(
89-
cranelift_module::FuncId::from_u32(sym.try_into().unwrap()),
90-
);
90+
let addr = if sym & 1 << 31 == 0 {
91+
let func_id = FuncId::from_u32(sym.try_into().unwrap());
92+
// FIXME make JITModule::get_address public and use it here instead.
93+
// HACK rust_eh_personality is likely not defined in the same crate,
94+
// so get_finalized_function won't work. Use the rust_eh_personality
95+
// of cg_clif itself, which is likely ABI compatible.
96+
if jit_module.declarations().get_function_decl(func_id).name.as_deref()
97+
== Some("rust_eh_personality")
98+
{
99+
extern "C" {
100+
fn rust_eh_personality() -> !;
101+
}
102+
rust_eh_personality as *const u8
103+
} else {
104+
jit_module.get_finalized_function(func_id)
105+
}
106+
} else {
107+
jit_module
108+
.get_finalized_data(DataId::from_u32(
109+
u32::try_from(sym).unwrap() & !(1 << 31),
110+
))
111+
.0
112+
};
113+
91114
let val = (addr as u64 as i64 + reloc.addend) as u64;
92115
self.writer.write_udata_at(reloc.offset as usize, val, reloc.size).unwrap();
93116
}
@@ -196,6 +219,16 @@ impl Writer for WriterRelocate {
196219
});
197220
self.write_udata(0, size)
198221
}
222+
gimli::DW_EH_PE_absptr => {
223+
self.relocs.push(DebugReloc {
224+
offset: self.len() as u32,
225+
size: size.into(),
226+
name: DebugRelocName::Symbol(symbol),
227+
addend,
228+
kind: object::RelocationKind::Absolute,
229+
});
230+
self.write_udata(0, size.into())
231+
}
199232
_ => Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)),
200233
},
201234
}

src/driver/jit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, jit_args: Vec<String>) -> ! {
8484

8585
tcx.dcx().abort_if_errors();
8686

87-
jit_module.finalize_definitions();
87+
let mut jit_module = jit_module.finalize_definitions();
8888

8989
println!(
9090
"Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed"
@@ -104,7 +104,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, jit_args: Vec<String>) -> ! {
104104
call_conv: jit_module.target_config().default_call_conv,
105105
};
106106
let start_func_id = jit_module.declare_function("main", Linkage::Import, &start_sig).unwrap();
107-
let finalized_start: *const u8 = jit_module.module.get_finalized_function(start_func_id);
107+
let finalized_start: *const u8 = jit_module.get_finalized_function(start_func_id);
108108

109109
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
110110
unsafe { ::std::mem::transmute(finalized_start) };

src/unwind_module.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,10 @@ impl UnwindModule<ObjectModule> {
3333

3434
#[cfg(feature = "jit")]
3535
impl UnwindModule<cranelift_jit::JITModule> {
36-
pub(crate) fn finalize_definitions(&mut self) {
36+
pub(crate) fn finalize_definitions(mut self) -> cranelift_jit::JITModule {
3737
self.module.finalize_definitions().unwrap();
38-
let prev_unwind_context = std::mem::replace(
39-
&mut self.unwind_context,
40-
UnwindContext::new(&mut self.module, false),
41-
);
42-
unsafe { prev_unwind_context.register_jit(&self.module) };
38+
unsafe { self.unwind_context.register_jit(&self.module) };
39+
self.module
4340
}
4441
}
4542

0 commit comments

Comments
 (0)