Home | History | Annotate | Download | only in dwarf
      1 // Copyright 2006 Google Inc. All Rights Reserved.
      2 //
      3 // Redistribution and use in source and binary forms, with or without
      4 // modification, are permitted provided that the following conditions are
      5 // met:
      6 //
      7 //     * Redistributions of source code must retain the above copyright
      8 // notice, this list of conditions and the following disclaimer.
      9 //     * Redistributions in binary form must reproduce the above
     10 // copyright notice, this list of conditions and the following disclaimer
     11 // in the documentation and/or other materials provided with the
     12 // distribution.
     13 //     * Neither the name of Google Inc. nor the names of its
     14 // contributors may be used to endorse or promote products derived from
     15 // this software without specific prior written permission.
     16 //
     17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 #ifndef UTIL_DEBUGINFO_BYTEREADER_INL_H__
     30 #define UTIL_DEBUGINFO_BYTEREADER_INL_H__
     31 
     32 #include "common/dwarf/bytereader.h"
     33 
     34 #include <assert.h>
     35 
     36 namespace dwarf2reader {
     37 
     38 inline uint8 ByteReader::ReadOneByte(const char* buffer) const {
     39   return buffer[0];
     40 }
     41 
     42 inline uint16 ByteReader::ReadTwoBytes(const char* signed_buffer) const {
     43   const unsigned char *buffer
     44     = reinterpret_cast<const unsigned char *>(signed_buffer);
     45   const uint16 buffer0 = buffer[0];
     46   const uint16 buffer1 = buffer[1];
     47   if (endian_ == ENDIANNESS_LITTLE) {
     48     return buffer0 | buffer1 << 8;
     49   } else {
     50     return buffer1 | buffer0 << 8;
     51   }
     52 }
     53 
     54 inline uint64 ByteReader::ReadFourBytes(const char* signed_buffer) const {
     55   const unsigned char *buffer
     56     = reinterpret_cast<const unsigned char *>(signed_buffer);
     57   const uint32 buffer0 = buffer[0];
     58   const uint32 buffer1 = buffer[1];
     59   const uint32 buffer2 = buffer[2];
     60   const uint32 buffer3 = buffer[3];
     61   if (endian_ == ENDIANNESS_LITTLE) {
     62     return buffer0 | buffer1 << 8 | buffer2 << 16 | buffer3 << 24;
     63   } else {
     64     return buffer3 | buffer2 << 8 | buffer1 << 16 | buffer0 << 24;
     65   }
     66 }
     67 
     68 inline uint64 ByteReader::ReadEightBytes(const char* signed_buffer) const {
     69   const unsigned char *buffer
     70     = reinterpret_cast<const unsigned char *>(signed_buffer);
     71   const uint64 buffer0 = buffer[0];
     72   const uint64 buffer1 = buffer[1];
     73   const uint64 buffer2 = buffer[2];
     74   const uint64 buffer3 = buffer[3];
     75   const uint64 buffer4 = buffer[4];
     76   const uint64 buffer5 = buffer[5];
     77   const uint64 buffer6 = buffer[6];
     78   const uint64 buffer7 = buffer[7];
     79   if (endian_ == ENDIANNESS_LITTLE) {
     80     return buffer0 | buffer1 << 8 | buffer2 << 16 | buffer3 << 24 |
     81       buffer4 << 32 | buffer5 << 40 | buffer6 << 48 | buffer7 << 56;
     82   } else {
     83     return buffer7 | buffer6 << 8 | buffer5 << 16 | buffer4 << 24 |
     84       buffer3 << 32 | buffer2 << 40 | buffer1 << 48 | buffer0 << 56;
     85   }
     86 }
     87 
     88 // Read an unsigned LEB128 number.  Each byte contains 7 bits of
     89 // information, plus one bit saying whether the number continues or
     90 // not.
     91 
     92 inline uint64 ByteReader::ReadUnsignedLEB128(const char* buffer,
     93                                              size_t* len) const {
     94   uint64 result = 0;
     95   size_t num_read = 0;
     96   unsigned int shift = 0;
     97   unsigned char byte;
     98 
     99   do {
    100     byte = *buffer++;
    101     num_read++;
    102 
    103     result |= (static_cast<uint64>(byte & 0x7f)) << shift;
    104 
    105     shift += 7;
    106 
    107   } while (byte & 0x80);
    108 
    109   *len = num_read;
    110 
    111   return result;
    112 }
    113 
    114 // Read a signed LEB128 number.  These are like regular LEB128
    115 // numbers, except the last byte may have a sign bit set.
    116 
    117 inline int64 ByteReader::ReadSignedLEB128(const char* buffer,
    118                                           size_t* len) const {
    119   int64 result = 0;
    120   unsigned int shift = 0;
    121   size_t num_read = 0;
    122   unsigned char byte;
    123 
    124   do {
    125       byte = *buffer++;
    126       num_read++;
    127       result |= (static_cast<uint64>(byte & 0x7f) << shift);
    128       shift += 7;
    129   } while (byte & 0x80);
    130 
    131   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
    132     result |= -((static_cast<int64>(1)) << shift);
    133   *len = num_read;
    134   return result;
    135 }
    136 
    137 inline uint64 ByteReader::ReadOffset(const char* buffer) const {
    138   assert(this->offset_reader_);
    139   return (this->*offset_reader_)(buffer);
    140 }
    141 
    142 inline uint64 ByteReader::ReadAddress(const char* buffer) const {
    143   assert(this->address_reader_);
    144   return (this->*address_reader_)(buffer);
    145 }
    146 
    147 inline void ByteReader::SetCFIDataBase(uint64 section_base,
    148                                        const char *buffer_base) {
    149   section_base_ = section_base;
    150   buffer_base_ = buffer_base;
    151   have_section_base_ = true;
    152 }
    153 
    154 inline void ByteReader::SetTextBase(uint64 text_base) {
    155   text_base_ = text_base;
    156   have_text_base_ = true;
    157 }
    158 
    159 inline void ByteReader::SetDataBase(uint64 data_base) {
    160   data_base_ = data_base;
    161   have_data_base_ = true;
    162 }
    163 
    164 inline void ByteReader::SetFunctionBase(uint64 function_base) {
    165   function_base_ = function_base;
    166   have_function_base_ = true;
    167 }
    168 
    169 inline void ByteReader::ClearFunctionBase() {
    170   have_function_base_ = false;
    171 }
    172 
    173 }  // namespace dwarf2reader
    174 
    175 #endif  // UTIL_DEBUGINFO_BYTEREADER_INL_H__
    176