Parse exception tables from a byte-slice as produced by [`ExceptionTableBuilder::serialize`].
(data: &'a [u8])
| 254 | /// Parse exception tables from a byte-slice as produced by |
| 255 | /// [`ExceptionTableBuilder::serialize`]. |
| 256 | pub fn parse(data: &'a [u8]) -> Result<ExceptionTable<'a>> { |
| 257 | let mut data = Bytes(data); |
| 258 | let callsite_count = data |
| 259 | .read::<U32<LittleEndian>>() |
| 260 | .map_err(|_| format_err!("Unable to read callsite count prefix"))?; |
| 261 | let callsite_count = usize::try_from(callsite_count.get(LittleEndian))?; |
| 262 | let handler_count = data |
| 263 | .read::<U32<LittleEndian>>() |
| 264 | .map_err(|_| format_err!("Unable to read handler count prefix"))?; |
| 265 | let handler_count = usize::try_from(handler_count.get(LittleEndian))?; |
| 266 | let (callsites, data) = |
| 267 | object::slice_from_bytes::<U32<LittleEndian>>(data.0, callsite_count) |
| 268 | .map_err(|_| format_err!("Unable to read callsites slice"))?; |
| 269 | let (frame_offsets, data) = |
| 270 | object::slice_from_bytes::<U32<LittleEndian>>(data, callsite_count) |
| 271 | .map_err(|_| format_err!("Unable to read frame_offsets slice"))?; |
| 272 | let (ranges, data) = object::slice_from_bytes::<U32<LittleEndian>>(data, callsite_count) |
| 273 | .map_err(|_| format_err!("Unable to read ranges slice"))?; |
| 274 | let (tags, data) = object::slice_from_bytes::<U32<LittleEndian>>(data, handler_count) |
| 275 | .map_err(|_| format_err!("Unable to read tags slice"))?; |
| 276 | let (contexts, data) = object::slice_from_bytes::<U32<LittleEndian>>(data, handler_count) |
| 277 | .map_err(|_| format_err!("Unable to read contexts slice"))?; |
| 278 | let (handlers, data) = object::slice_from_bytes::<U32<LittleEndian>>(data, handler_count) |
| 279 | .map_err(|_| format_err!("Unable to read handlers slice"))?; |
| 280 | |
| 281 | if !data.is_empty() { |
| 282 | bail!("Unexpected data at end of serialized exception table"); |
| 283 | } |
| 284 | |
| 285 | Ok(ExceptionTable { |
| 286 | callsites, |
| 287 | frame_offsets, |
| 288 | ranges, |
| 289 | tags, |
| 290 | contexts, |
| 291 | handlers, |
| 292 | }) |
| 293 | } |
| 294 | |
| 295 | /// Look up the set of handlers, if any, for a given return |
| 296 | /// address (as an offset into the code section). |