Home | History | Annotate | Download | only in windows
      1 // Copyright 2013 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 // Declares internal implementation details for functionality in omap.h and
     30 // omap.cc.
     31 
     32 #ifndef COMMON_WINDOWS_OMAP_INTERNAL_H_
     33 #define COMMON_WINDOWS_OMAP_INTERNAL_H_
     34 
     35 #include <windows.h>
     36 #include <dia2.h>
     37 
     38 #include <vector>
     39 
     40 namespace google_breakpad {
     41 
     42 // The OMAP struct is defined by debughlp.h, which doesn't play nicely with
     43 // imagehlp.h. We simply redefine it.
     44 struct OMAP {
     45   DWORD rva;
     46   DWORD rvaTo;
     47 };
     48 static_assert(sizeof(OMAP) == 8, "Wrong size for OMAP structure.");
     49 typedef std::vector<OMAP> OmapTable;
     50 
     51 // This contains the OMAP data extracted from an image.
     52 struct OmapData {
     53   // The table of OMAP entries describing the transformation from the
     54   // original image to the transformed image.
     55   OmapTable omap_from;
     56   // The table of OMAP entries describing the transformation from the
     57   // instrumented image to the original image.
     58   OmapTable omap_to;
     59   // The length of the original untransformed image.
     60   DWORD length_original;
     61 
     62   OmapData() : length_original(0) { }
     63 };
     64 
     65 // This represents a range of addresses in an image.
     66 struct AddressRange {
     67   DWORD rva;
     68   DWORD length;
     69 
     70   AddressRange() : rva(0), length(0) { }
     71   AddressRange(DWORD rva, DWORD length) : rva(rva), length(length) { }
     72 
     73   // Returns the end address of this range.
     74   DWORD end() const { return rva + length; }
     75 
     76   // Addreses only compare as less-than or greater-than if they are not
     77   // overlapping. Otherwise, they compare equal.
     78   int Compare(const AddressRange& rhs) const;
     79   bool operator<(const AddressRange& rhs) const { return Compare(rhs) == -1; }
     80   bool operator>(const AddressRange& rhs) const { return Compare(rhs) == 1; }
     81 
     82   // Equality operators compare exact values.
     83   bool operator==(const AddressRange& rhs) const {
     84     return rva == rhs.rva && length == rhs.length;
     85   }
     86   bool operator!=(const  AddressRange& rhs) const { return !((*this) == rhs); }
     87 };
     88 
     89 typedef std::vector<AddressRange> AddressRangeVector;
     90 
     91 // This represents an address range in an original image, and its corresponding
     92 // range in the transformed image.
     93 struct MappedRange {
     94   // An address in the original image.
     95   DWORD rva_original;
     96   // The corresponding addresses in the transformed image.
     97   DWORD rva_transformed;
     98   // The length of the address range.
     99   DWORD length;
    100   // It is possible for code to be injected into a transformed image, for which
    101   // there is no corresponding code in the original image. If this range of
    102   // transformed image is immediately followed by such injected code we maintain
    103   // a record of its length here.
    104   DWORD injected;
    105   // It is possible for code to be removed from the original image. This happens
    106   // for things like padding between blocks. There is no actual content lost,
    107   // but the spacing between items may be lost. This keeps track of any removed
    108   // content immediately following the |original| range.
    109   DWORD removed;
    110 };
    111 // A vector of mapped ranges is used as a more useful representation of
    112 // OMAP data.
    113 typedef std::vector<MappedRange> Mapping;
    114 
    115 // Used as a secondary search structure accompanying a Mapping.
    116 struct EndpointIndex {
    117   DWORD endpoint;
    118   size_t index;
    119 };
    120 typedef std::vector<EndpointIndex> EndpointIndexMap;
    121 
    122 // An ImageMap is vector of mapped ranges, plus a secondary index into it for
    123 // doing interval searches. (An interval tree would also work, but is overkill
    124 // because we don't need insertion and deletion.)
    125 struct ImageMap {
    126   // This is a description of the mapping between original and transformed
    127   // image, sorted by addresses in the original image.
    128   Mapping mapping;
    129   // For all interval endpoints in |mapping| this stores the minimum index of
    130   // an interval in |mapping| that contains the endpoint. Useful for doing
    131   // interval intersection queries.
    132   EndpointIndexMap endpoint_index_map;
    133 };
    134 
    135 }  // namespace google_breakpad
    136 
    137 #endif  // COMMON_WINDOWS_OMAP_INTERNAL_H_
    138