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 // 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 base address, and is the | |
| 22 // preferred address for pointers in an image. | |
| 23 // In Courgette we are mostly interested in FILE Offsets and RVAs. | |
| 24 | |
| 25 using RVA = uint32_t; | |
| 18 const RVA kUnassignedRVA = 0xFFFFFFFFU; | 26 const RVA kUnassignedRVA = 0xFFFFFFFFU; |
| 27 const RVA kNoRVA = 0xFFFFFFFFU; | |
| 28 | |
| 29 using FileOffset = size_t; | |
|
grt (UTC plus 2)
2016/02/11 16:55:34
is ptrdiff_t (the type resulting from subtracting
huangs
2016/02/12 01:08:42
Seems more correct. However, when I implement thi
| |
| 30 const FileOffset kNoFileOffset = 0xFFFFFFFFU; | |
|
grt (UTC plus 2)
2016/02/11 16:55:34
this should be something like PTRDIFF_MAX (if File
huangs
2016/02/12 01:08:42
Done (UINTPTR_MAX for size_t).
| |
| 31 | |
| 32 // An interface for {RVA, File Offset, pointer to image data} translation. | |
| 33 class AddressTranslator { | |
|
grt (UTC plus 2)
2016/02/11 16:55:34
will this eventually:
- be implemented by types ot
huangs
2016/02/12 01:08:42
Yes. The plan is to create multiple subclasses ( p
| |
| 34 public: | |
| 35 // Returns the RVA corresponding to |file_offset|, or kNoRVA if nonexistent. | |
| 36 virtual RVA FileOffsetToRVA(FileOffset file_offset) const = 0; | |
| 37 | |
| 38 // Returns the file offset corresponding to |rva|, or kFileNoOffset if | |
| 39 // nonexistent. | |
| 40 virtual FileOffset RVAToFileOffset(RVA rva) const = 0; | |
| 41 | |
| 42 // Returns the pointer to the image data for |file_offset|. Assumes that | |
| 43 // 0 <= |file_offset| <= image size. We allow |file_offset| == image size to | |
| 44 // enable "end" bound for iteration. | |
| 45 virtual const uint8_t* FileOffsetToPointer(FileOffset file_offset) const = 0; | |
| 46 | |
| 47 // Returns the pointer to the image data for |rva|, or null if |rva| is | |
| 48 // invalid. | |
| 49 virtual const uint8_t* RVAToPointer(RVA rva) const = 0; | |
| 50 }; | |
| 19 | 51 |
| 20 // A Label is a symbolic reference to an address. Unlike a conventional | 52 // 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 | 53 // 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 | 54 // stored in a table and the Label will be replaced with the index into the |
| 23 // table. | 55 // table. |
| 24 // TODO(huangs): Make this a struct, and remove "_" from member names. | 56 // TODO(huangs): Make this a struct, and remove "_" from member names. |
| 25 class Label { | 57 class Label { |
| 26 public: | 58 public: |
| 27 enum : int { kNoIndex = -1 }; | 59 enum : int { kNoIndex = -1 }; |
| 28 explicit Label(RVA rva) : rva_(rva) {} | 60 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); | 93 return *reinterpret_cast<const uint32_t*>(address); |
| 62 } | 94 } |
| 63 | 95 |
| 64 inline uint64_t Read64LittleEndian(const void* address) { | 96 inline uint64_t Read64LittleEndian(const void* address) { |
| 65 return *reinterpret_cast<const uint64_t*>(address); | 97 return *reinterpret_cast<const uint64_t*>(address); |
| 66 } | 98 } |
| 67 | 99 |
| 68 } // namespace courgette | 100 } // namespace courgette |
| 69 | 101 |
| 70 #endif // COURGETTE_IMAGE_UTILS_H_ | 102 #endif // COURGETTE_IMAGE_UTILS_H_ |
| OLD | NEW |