1 //! Utilities for parsing DWARF-encoded data streams.
2 //! See <http://www.dwarfstd.org>,
3 //! DWARF-4 standard, Section 7 - "Data Representation"
5 // This module is used only by x86_64-pc-windows-gnu for now, but we
6 // are compiling it everywhere to avoid regressions.
16 pub struct DwarfReader {
21 struct Unaligned<T>(T);
24 pub fn new(ptr: *const u8) -> DwarfReader {
28 // DWARF streams are packed, so e.g., a u32 would not necessarily be aligned
29 // on a 4-byte boundary. This may cause problems on platforms with strict
30 // alignment requirements. By wrapping data in a "packed" struct, we are
31 // telling the backend to generate "misalignment-safe" code.
32 pub unsafe fn read<T: Copy>(&mut self) -> T {
33 let Unaligned(result) = *(self.ptr as *const Unaligned<T>);
34 self.ptr = self.ptr.add(mem::size_of::<T>());
38 // ULEB128 and SLEB128 encodings are defined in Section 7.6 - "Variable
40 pub unsafe fn read_uleb128(&mut self) -> u64 {
41 let mut shift: usize = 0;
42 let mut result: u64 = 0;
45 byte = self.read::<u8>();
46 result |= ((byte & 0x7F) as u64) << shift;
55 pub unsafe fn read_sleb128(&mut self) -> i64 {
56 let mut shift: u32 = 0;
57 let mut result: u64 = 0;
60 byte = self.read::<u8>();
61 result |= ((byte & 0x7F) as u64) << shift;
68 if shift < u64::BITS && (byte & 0x40) != 0 {
69 result |= (!0 as u64) << shift;