| 1 |
[](https://crates.io/crates/macho-unwind-info) |
| 2 |
[](https://docs.rs/macho-unwind-info/) |
| 3 |
|
| 4 |
# macho-unwind-info |
| 5 |
|
| 6 |
A zero-copy parser for the contents of the `__unwind_info` section of a |
| 7 |
mach-O binary. |
| 8 |
|
| 9 |
Quickly look up the unwinding opcode for an address. Then parse the opcode to find |
| 10 |
out how to recover the return address and the caller frame's register values. |
| 11 |
|
| 12 |
This crate is intended to be fast enough to be used in a sampling profiler. |
| 13 |
Re-parsing from scratch is cheap and can be done on every sample. |
| 14 |
|
| 15 |
For the full unwinding experience, both `__unwind_info` and `__eh_frame` may need |
| 16 |
to be consulted. The two sections are complementary: `__unwind_info` handles the |
| 17 |
easy cases, and refers to an `__eh_frame` FDE for the hard cases. Conversely, |
| 18 |
`__eh_frame` only includes FDEs for functions whose unwinding info cannot be |
| 19 |
represented in `__unwind_info`. |
| 20 |
|
| 21 |
On x86 and x86_64, `__unwind_info` can represent most functions regardless of |
| 22 |
whether they were compiled with framepointers or without. |
| 23 |
|
| 24 |
On arm64, compiling without framepointers is strongly discouraged, and |
| 25 |
`__unwind_info` can only represent functions which have framepointers or |
| 26 |
which don't need to restore any registers. As a result, if you have an arm64 |
| 27 |
binary without framepointers (rare!), then the `__unwind_info` basically just |
| 28 |
acts as an index for `__eh_frame`, similarly to `.eh_frame_hdr` for ELF. |
| 29 |
|
| 30 |
In clang's default configuration for arm64, non-leaf functions have framepointers |
| 31 |
and leaf functions without stored registers on the stack don't have framepointers. |
| 32 |
For leaf functions, the return address is kept in the `lr` register for the entire |
| 33 |
duration of the function. And the unwind info lets you discern between these two |
| 34 |
types of functions ("frame-based" and "frameless"). |
| 35 |
|
| 36 |
## Example |
| 37 |
|
| 38 |
```rust |
| 39 |
use macho_unwind_info::UnwindInfo; |
| 40 |
use macho_unwind_info::opcodes::OpcodeX86_64; |
| 41 |
|
| 42 |
let unwind_info = UnwindInfo::parse(data)?; |
| 43 |
|
| 44 |
if let Some(function) = unwind_info.lookup(0x1234)? { |
| 45 |
println!("Found function entry covering the address 0x1234:"); |
| 46 |
let opcode = OpcodeX86_64::parse(function.opcode); |
| 47 |
println!("0x{:08x}..0x{:08x}: {}", function.start_address, function.end_address, opcode); |
| 48 |
} |
| 49 |
``` |
| 50 |
|
| 51 |
## Command-line usage |
| 52 |
|
| 53 |
This repository also contains two CLI executables. You can install them like so: |
| 54 |
|
| 55 |
``` |
| 56 |
% cargo install --examples macho-unwind-info |
| 57 |
``` |
| 58 |
|
| 59 |
## Acknowledgements |
| 60 |
|
| 61 |
Thanks a ton to [**@Gankra**](https://github.com/Gankra/) for documenting this format at https://gankra.github.io/blah/compact-unwinding/. |
| 62 |
|
| 63 |
## License |
| 64 |
|
| 65 |
Licensed under either of |
| 66 |
|
| 67 |
* Apache License, Version 2.0 ([`LICENSE-APACHE`](./LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 68 |
* MIT license ([`LICENSE-MIT`](./LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 69 |
|
| 70 |
at your option. |
| 71 |
|
| 72 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 73 |
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 74 |
dual licensed as above, without any additional terms or conditions. |