-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Write .debug_frame information #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yurydelendik
merged 3 commits into
bytecodealliance:master
from
yurydelendik:debug-frame
Mar 11, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| use crate::transform::map_reg; | ||
| use std::collections::HashMap; | ||
| use wasmtime_environ::entity::EntityRef; | ||
| use wasmtime_environ::isa::{CallConv, TargetIsa}; | ||
| use wasmtime_environ::wasm::DefinedFuncIndex; | ||
| use wasmtime_environ::{FrameLayoutChange, FrameLayouts}; | ||
|
|
||
| use gimli::write::{ | ||
| Address, CallFrameInstruction, CommonInformationEntry as CIEEntry, Error, | ||
| FrameDescriptionEntry as FDEEntry, FrameTable, | ||
| }; | ||
| use gimli::{Encoding, Format, Register, X86_64}; | ||
|
|
||
| fn to_cfi( | ||
| isa: &dyn TargetIsa, | ||
| change: &FrameLayoutChange, | ||
| cfa_def_reg: &mut Register, | ||
| cfa_def_offset: &mut i32, | ||
| ) -> Option<CallFrameInstruction> { | ||
| Some(match change { | ||
| FrameLayoutChange::CallFrameAddressAt { reg, offset } => { | ||
| let mapped = match map_reg(isa, *reg) { | ||
| Ok(r) => r, | ||
| Err(_) => return None, | ||
| }; | ||
| let offset = (*offset) as i32; | ||
| if mapped != *cfa_def_reg && offset != *cfa_def_offset { | ||
| *cfa_def_reg = mapped; | ||
| *cfa_def_offset = offset; | ||
| CallFrameInstruction::Cfa(mapped, offset) | ||
| } else if offset != *cfa_def_offset { | ||
| *cfa_def_offset = offset; | ||
| CallFrameInstruction::CfaOffset(offset) | ||
| } else if mapped != *cfa_def_reg { | ||
| *cfa_def_reg = mapped; | ||
| CallFrameInstruction::CfaRegister(mapped) | ||
| } else { | ||
| return None; | ||
| } | ||
| } | ||
| FrameLayoutChange::RegAt { reg, cfa_offset } => { | ||
| assert!(cfa_offset % -8 == 0); | ||
| let cfa_offset = *cfa_offset as i32; | ||
| let mapped = match map_reg(isa, *reg) { | ||
| Ok(r) => r, | ||
| Err(_) => return None, | ||
| }; | ||
| CallFrameInstruction::Offset(mapped, cfa_offset) | ||
| } | ||
| FrameLayoutChange::ReturnAddressAt { cfa_offset } => { | ||
| assert!(cfa_offset % -8 == 0); | ||
| let cfa_offset = *cfa_offset as i32; | ||
| CallFrameInstruction::Offset(X86_64::RA, cfa_offset) | ||
| } | ||
| FrameLayoutChange::Preserve => CallFrameInstruction::RememberState, | ||
| FrameLayoutChange::Restore => CallFrameInstruction::RestoreState, | ||
| }) | ||
| } | ||
|
|
||
| pub fn get_debug_frame_bytes( | ||
| funcs: &[(*const u8, usize)], | ||
| isa: &dyn TargetIsa, | ||
| layouts: &FrameLayouts, | ||
| ) -> Result<Option<FrameTable>, Error> { | ||
| // FIXME Only x86-64 at this moment. | ||
| if isa.name() != "x86" || isa.pointer_bits() != 64 { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| let address_size = isa.pointer_bytes(); | ||
| let encoding = Encoding { | ||
| format: Format::Dwarf64, | ||
| version: 4, | ||
| address_size, | ||
| }; | ||
|
|
||
| let mut frames = FrameTable::default(); | ||
|
|
||
| let mut cached_cies = HashMap::new(); | ||
|
|
||
| for (i, f) in funcs.into_iter().enumerate() { | ||
| let layout = &layouts[DefinedFuncIndex::new(i)]; | ||
|
|
||
| // FIXME Can only process functions with SystemV-like prologue. | ||
| if layout.call_conv != CallConv::Fast | ||
| && layout.call_conv != CallConv::Cold | ||
| && layout.call_conv != CallConv::SystemV | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Caching CIE with similar initial_commands. | ||
| let (cie_id, mut cfa_def_reg, mut cfa_def_offset) = { | ||
| use std::collections::hash_map::Entry; | ||
| match cached_cies.entry(&layout.initial_commands) { | ||
| Entry::Occupied(o) => *o.get(), | ||
| Entry::Vacant(v) => { | ||
| // cfa_def_reg and cfa_def_offset initialized with some random values. | ||
| let mut cfa_def_reg = X86_64::RA; | ||
| let mut cfa_def_offset = 0i32; | ||
|
|
||
| // TODO adjust code_alignment_factor and data_alignment_factor based on ISA. | ||
| let mut cie = CIEEntry::new( | ||
| encoding, | ||
| /* code_alignment_factor = */ 1, | ||
| /* data_alignment_factor = */ -8, | ||
| /* return_address_register = */ X86_64::RA, | ||
| ); | ||
| for cmd in layout.initial_commands.iter() { | ||
| if let Some(instr) = to_cfi(isa, cmd, &mut cfa_def_reg, &mut cfa_def_offset) | ||
| { | ||
| cie.add_instruction(instr); | ||
| } | ||
| } | ||
| let cie_id = frames.add_cie(cie); | ||
| *v.insert((cie_id, cfa_def_reg, cfa_def_offset)) | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| let f_len = f.1 as u32; | ||
| let mut fde = FDEEntry::new( | ||
| Address::Symbol { | ||
| symbol: i, | ||
| addend: 0, | ||
| }, | ||
| f_len, | ||
| ); | ||
|
|
||
| for (offset, cmd) in layout.commands.into_iter() { | ||
| if let Some(instr) = to_cfi(isa, cmd, &mut cfa_def_reg, &mut cfa_def_offset) { | ||
| fde.add_instruction(*offset as u32, instr); | ||
| } | ||
| } | ||
|
|
||
| frames.add_fde(cie_id, fde); | ||
| } | ||
|
|
||
| Ok(Some(frames)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| use anyhow::{bail, Result}; | ||
| use gimli::{Register, X86_64}; | ||
| use wasmtime_environ::isa::{RegUnit, TargetIsa}; | ||
|
|
||
| pub(crate) fn map_reg(isa: &dyn TargetIsa, reg: RegUnit) -> Result<Register> { | ||
| // TODO avoid duplication with fde.rs | ||
| assert!(isa.name() == "x86" && isa.pointer_bits() == 64); | ||
| // Mapping from https://github.com/bytecodealliance/cranelift/pull/902 by @iximeow | ||
| const X86_GP_REG_MAP: [Register; 16] = [ | ||
| X86_64::RAX, | ||
| X86_64::RCX, | ||
| X86_64::RDX, | ||
| X86_64::RBX, | ||
| X86_64::RSP, | ||
| X86_64::RBP, | ||
| X86_64::RSI, | ||
| X86_64::RDI, | ||
| X86_64::R8, | ||
| X86_64::R9, | ||
| X86_64::R10, | ||
| X86_64::R11, | ||
| X86_64::R12, | ||
| X86_64::R13, | ||
| X86_64::R14, | ||
| X86_64::R15, | ||
| ]; | ||
| const X86_XMM_REG_MAP: [Register; 16] = [ | ||
| X86_64::XMM0, | ||
| X86_64::XMM1, | ||
| X86_64::XMM2, | ||
| X86_64::XMM3, | ||
| X86_64::XMM4, | ||
| X86_64::XMM5, | ||
| X86_64::XMM6, | ||
| X86_64::XMM7, | ||
| X86_64::XMM8, | ||
| X86_64::XMM9, | ||
| X86_64::XMM10, | ||
| X86_64::XMM11, | ||
| X86_64::XMM12, | ||
| X86_64::XMM13, | ||
| X86_64::XMM14, | ||
| X86_64::XMM15, | ||
| ]; | ||
| let reg_info = isa.register_info(); | ||
| let bank = reg_info.bank_containing_regunit(reg).unwrap(); | ||
| match bank.name { | ||
| "IntRegs" => { | ||
| // x86 GP registers have a weird mapping to DWARF registers, so we use a | ||
| // lookup table. | ||
| Ok(X86_GP_REG_MAP[(reg - bank.first_unit) as usize]) | ||
| } | ||
| "FloatRegs" => Ok(X86_XMM_REG_MAP[(reg - bank.first_unit) as usize]), | ||
| bank_name => { | ||
| bail!("unsupported register bank: {}", bank_name); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.