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