| 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 // COURGETTE_HISTOGRAM_TARGETS prints out a histogram of how frequently | 11 // COURGETTE_HISTOGRAM_TARGETS prints out a histogram of how frequently |
| 12 // different target addresses are referenced. Purely for debugging. | 12 // different target addresses are referenced. Purely for debugging. |
| 13 #define COURGETTE_HISTOGRAM_TARGETS 0 | 13 #define COURGETTE_HISTOGRAM_TARGETS 0 |
| 14 | 14 |
| 15 namespace courgette { | 15 namespace courgette { |
| 16 | 16 |
| 17 typedef uint32_t RVA; | 17 // There are several ways to reason about addresses in an image: |
| 18 // - File Offset: Position relative to start of image. |
| 19 // - VA (Virtual Address): Virtual memory address of a loaded image. This is |
| 20 // subject to relocation by the OS. |
| 21 // - RVA (Relative Virtual Address): VA relative to some base address. This is |
| 22 // the preferred way to specify pointers in an image. Two ways to encode RVA |
| 23 // are: |
| 24 // - abs32: RVA value is encoded directly. |
| 25 // - rel32: RVA is encoded as offset from an instruction address. This is |
| 26 // commonly used for relative branch/call opcodes. |
| 27 // Courgette operates on File Offsets and RVAs only. |
| 28 |
| 29 using RVA = uint32_t; |
| 18 const RVA kUnassignedRVA = 0xFFFFFFFFU; | 30 const RVA kUnassignedRVA = 0xFFFFFFFFU; |
| 31 const RVA kNoRVA = 0xFFFFFFFFU; |
| 32 |
| 33 using FileOffset = size_t; |
| 34 const FileOffset kNoFileOffset = UINTPTR_MAX; |
| 35 |
| 36 // An interface for {File Offset, RVA, pointer to image data} translation. |
| 37 class AddressTranslator { |
| 38 public: |
| 39 // Returns the RVA corresponding to |file_offset|, or kNoRVA if nonexistent. |
| 40 virtual RVA FileOffsetToRVA(FileOffset file_offset) const = 0; |
| 41 |
| 42 // Returns the file offset corresponding to |rva|, or kNoFileOffset if |
| 43 // nonexistent. |
| 44 virtual FileOffset RVAToFileOffset(RVA rva) const = 0; |
| 45 |
| 46 // Returns the pointer to the image data for |file_offset|. Assumes that |
| 47 // 0 <= |file_offset| <= image size. If |file_offset| == image, the resulting |
| 48 // pointer is an end bound for iteration that should never be dereferenced. |
| 49 virtual const uint8_t* FileOffsetToPointer(FileOffset file_offset) const = 0; |
| 50 |
| 51 // Returns the pointer to the image data for |rva|, or null if |rva| is |
| 52 // invalid. |
| 53 virtual const uint8_t* RVAToPointer(RVA rva) const = 0; |
| 54 }; |
| 19 | 55 |
| 20 // A Label is a symbolic reference to an address. Unlike a conventional | 56 // A Label is a symbolic reference to an address. Unlike a conventional |
| 21 // assembly language, we always know the address. The address will later be | 57 // assembly language, we always know the address. The address will later be |
| 22 // stored in a table and the Label will be replaced with the index into the | 58 // stored in a table and the Label will be replaced with the index into the |
| 23 // table. | 59 // table. |
| 24 // TODO(huangs): Make this a struct, and remove "_" from member names. | 60 // TODO(huangs): Make this a struct, and remove "_" from member names. |
| 25 class Label { | 61 class Label { |
| 26 public: | 62 public: |
| 27 enum : int { kNoIndex = -1 }; | 63 enum : int { kNoIndex = -1 }; |
| 28 explicit Label(RVA rva) : rva_(rva) {} | 64 explicit Label(RVA rva) : rva_(rva) {} |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 return *reinterpret_cast<const uint32_t*>(address); | 97 return *reinterpret_cast<const uint32_t*>(address); |
| 62 } | 98 } |
| 63 | 99 |
| 64 inline uint64_t Read64LittleEndian(const void* address) { | 100 inline uint64_t Read64LittleEndian(const void* address) { |
| 65 return *reinterpret_cast<const uint64_t*>(address); | 101 return *reinterpret_cast<const uint64_t*>(address); |
| 66 } | 102 } |
| 67 | 103 |
| 68 } // namespace courgette | 104 } // namespace courgette |
| 69 | 105 |
| 70 #endif // COURGETTE_IMAGE_UTILS_H_ | 106 #endif // COURGETTE_IMAGE_UTILS_H_ |
| OLD | NEW |