Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COURGETTE_IMAGE_UTILS_H_ | 5 #ifndef COURGETTE_IMAGE_UTILS_H_ |
| 6 #define COURGETTE_IMAGE_UTILS_H_ | 6 #define COURGETTE_IMAGE_UTILS_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <iterator> | |
| 12 #include <vector> | |
| 13 | |
| 11 // COURGETTE_HISTOGRAM_TARGETS prints out a histogram of how frequently | 14 // COURGETTE_HISTOGRAM_TARGETS prints out a histogram of how frequently |
| 12 // different target addresses are referenced. Purely for debugging. | 15 // different target addresses are referenced. Purely for debugging. |
| 13 #define COURGETTE_HISTOGRAM_TARGETS 0 | 16 #define COURGETTE_HISTOGRAM_TARGETS 0 |
| 14 | 17 |
| 15 namespace courgette { | 18 namespace courgette { |
| 16 | 19 |
| 17 // There are several ways to reason about addresses in an image: | 20 // There are several ways to reason about addresses in an image: |
| 18 // - File Offset: Position relative to start of image. | 21 // - File Offset: Position relative to start of image. |
| 19 // - VA (Virtual Address): Virtual memory address of a loaded image. This is | 22 // - VA (Virtual Address): Virtual memory address of a loaded image. This is |
| 20 // subject to relocation by the OS. | 23 // subject to relocation by the OS. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 bool operator==(const Label& other) const { | 86 bool operator==(const Label& other) const { |
| 84 return rva_ == other.rva_ && index_ == other.index_ && | 87 return rva_ == other.rva_ && index_ == other.index_ && |
| 85 count_ == other.count_; | 88 count_ == other.count_; |
| 86 } | 89 } |
| 87 | 90 |
| 88 RVA rva_ = kUnassignedRVA; // Address referred to by the label. | 91 RVA rva_ = kUnassignedRVA; // Address referred to by the label. |
| 89 int index_ = kNoIndex; // Index of address in address table. | 92 int index_ = kNoIndex; // Index of address in address table. |
| 90 int32_t count_ = 0; | 93 int32_t count_ = 0; |
| 91 }; | 94 }; |
| 92 | 95 |
| 96 // An interface for sequential visit of RVAs. | |
| 97 // Use case: Translating from RVA locations to RVA targets is platform-specific, | |
| 98 // and works differently for abs32 vs. rel32. A function that sequentually | |
| 99 // visits RVA targets only requires an RvaVisitor. The caller can provide an | |
| 100 // implementation that stores a fixed list of RVA locations, and translates each | |
| 101 // to the matching RVA target on demand without extra storage. | |
| 102 class RvaVisitor { | |
| 103 public: | |
| 104 // Returns the number of remaining RVAs to visit. | |
| 105 virtual size_t Remaining() const = 0; | |
| 106 | |
| 107 // Returns the current RVA. | |
| 108 virtual RVA Get() const = 0; | |
| 109 | |
| 110 // Advances to the next RVA. | |
| 111 virtual void Next() = 0; | |
| 112 }; | |
| 113 | |
| 114 // RvaVisitor whose data are backed by std::vector<T>. Translating from T to RVA | |
| 115 // is should be implemented in Get(). | |
| 116 template <typename T> | |
| 117 class VectorRvaVisitor : public RvaVisitor { | |
| 118 public: | |
| 119 // Assumes |v| does not change for the lifetime of this instance. | |
| 120 explicit VectorRvaVisitor(const std::vector<T>& v) | |
| 121 : it_(v.begin()), end_(v.end()) {} | |
| 122 | |
| 123 // RvaVisitor interfaces. | |
| 124 size_t Remaining() const override { return std::distance(it_, end_); } | |
| 125 virtual RVA Get() const override = 0; | |
| 126 void Next() override { ++it_; } | |
| 127 | |
| 128 protected: | |
| 129 typename std::vector<T>::const_iterator it_; | |
| 130 typename std::vector<T>::const_iterator end_; | |
| 131 }; | |
| 132 | |
| 133 // RvaVisitor that simply stores a list of RVAs for traversal. For testing. | |
|
huangs
2016/04/04 14:30:58
We'll reuse it in adjustment_method_unittest.cc --
| |
| 134 class TrivialRvaVisitor : public VectorRvaVisitor<RVA> { | |
| 135 public: | |
| 136 explicit TrivialRvaVisitor(const std::vector<RVA>& rvas) | |
| 137 : VectorRvaVisitor<RVA>(rvas) {} | |
| 138 | |
| 139 // VectorRvaVisitor<RVA> interfaces. | |
| 140 RVA Get() const override { return *it_; } | |
| 141 }; | |
| 142 | |
| 93 // These helper functions avoid the need for casts in the main code. | 143 // These helper functions avoid the need for casts in the main code. |
| 94 inline uint16_t ReadU16(const uint8_t* address, size_t offset) { | 144 inline uint16_t ReadU16(const uint8_t* address, size_t offset) { |
| 95 return *reinterpret_cast<const uint16_t*>(address + offset); | 145 return *reinterpret_cast<const uint16_t*>(address + offset); |
| 96 } | 146 } |
| 97 | 147 |
| 98 inline uint32_t ReadU32(const uint8_t* address, size_t offset) { | 148 inline uint32_t ReadU32(const uint8_t* address, size_t offset) { |
| 99 return *reinterpret_cast<const uint32_t*>(address + offset); | 149 return *reinterpret_cast<const uint32_t*>(address + offset); |
| 100 } | 150 } |
| 101 | 151 |
| 102 inline uint64_t ReadU64(const uint8_t* address, size_t offset) { | 152 inline uint64_t ReadU64(const uint8_t* address, size_t offset) { |
| 103 return *reinterpret_cast<const uint64_t*>(address + offset); | 153 return *reinterpret_cast<const uint64_t*>(address + offset); |
| 104 } | 154 } |
| 105 | 155 |
| 106 inline uint16_t Read16LittleEndian(const void* address) { | 156 inline uint16_t Read16LittleEndian(const void* address) { |
| 107 return *reinterpret_cast<const uint16_t*>(address); | 157 return *reinterpret_cast<const uint16_t*>(address); |
| 108 } | 158 } |
| 109 | 159 |
| 110 inline uint32_t Read32LittleEndian(const void* address) { | 160 inline uint32_t Read32LittleEndian(const void* address) { |
| 111 return *reinterpret_cast<const uint32_t*>(address); | 161 return *reinterpret_cast<const uint32_t*>(address); |
| 112 } | 162 } |
| 113 | 163 |
| 114 inline uint64_t Read64LittleEndian(const void* address) { | 164 inline uint64_t Read64LittleEndian(const void* address) { |
| 115 return *reinterpret_cast<const uint64_t*>(address); | 165 return *reinterpret_cast<const uint64_t*>(address); |
| 116 } | 166 } |
| 117 | 167 |
| 118 } // namespace courgette | 168 } // namespace courgette |
| 119 | 169 |
| 120 #endif // COURGETTE_IMAGE_UTILS_H_ | 170 #endif // COURGETTE_IMAGE_UTILS_H_ |
| OLD | NEW |