@@ -17,32 +17,30 @@ pub struct DwarfReader {
1717pub ptr : * const u8 ,
1818}
1919
20- #[ repr( C , packed) ]
21- struct Unaligned < T > ( T ) ;
22-
20+ #[ forbid( unsafe_op_in_unsafe_fn) ]
2321impl DwarfReader {
2422pub fn new ( ptr : * const u8 ) ->DwarfReader {
2523DwarfReader { ptr}
2624}
2725
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.
26+ /// Read a type T and then bump the pointer by that amount.
27+ ///
28+ /// DWARF streams are "packed", so all types must be read at align 1.
3229pub 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 > ( ) ) ;
35- result
30+ unsafe {
31+ let result =self . ptr . cast :: < T > ( ) . read_unaligned ( ) ;
32+ self . ptr =self . ptr . byte_add ( mem:: size_of :: < T > ( ) ) ;
33+ result
34+ }
3635}
3736
38- // ULEB128 and SLEB128 encodings are defined in Section 7.6 - "Variable
39- // Length Data".
37+ /// ULEB128 and SLEB128 encodings are defined in Section 7.6 - "Variable Length Data".
4038pub unsafe fn read_uleb128 ( & mut self ) ->u64 {
4139let mut shift: usize =0 ;
4240let mut result: u64 =0 ;
4341let mut byte: u8 ;
4442loop {
45- byte =self . read :: < u8 > ( ) ;
43+ byte =unsafe { self . read :: < u8 > ( ) } ;
4644 result |=( ( byte& 0x7F ) as u64 ) << shift;
4745 shift +=7 ;
4846if byte& 0x80 ==0 {
@@ -57,7 +55,7 @@ impl DwarfReader {
5755let mut result: u64 =0 ;
5856let mut byte: u8 ;
5957loop {
60- byte =self . read :: < u8 > ( ) ;
58+ byte =unsafe { self . read :: < u8 > ( ) } ;
6159 result |=( ( byte& 0x7F ) as u64 ) << shift;
6260 shift +=7 ;
6361if byte& 0x80 ==0 {