| 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_REL32_FINDER_WIN32_X86_H_ | 5 #ifndef COURGETTE_REL32_FINDER_WIN32_X86_H_ |
| 6 #define COURGETTE_REL32_FINDER_WIN32_X86_H_ | 6 #define COURGETTE_REL32_FINDER_WIN32_X86_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #if COURGETTE_HISTOGRAM_TARGETS | |
| 11 #include <map> | 10 #include <map> |
| 12 #endif | |
| 13 #include <vector> | 11 #include <vector> |
| 14 | 12 |
| 15 #include "courgette/image_utils.h" | 13 #include "courgette/image_utils.h" |
| 16 | 14 |
| 17 namespace courgette { | 15 namespace courgette { |
| 18 | 16 |
| 19 // A helper class to scan through a section of code to extract RVAs. | 17 // A helper class to scan through a section of code to extract RVAs. |
| 20 class Rel32FinderWin32X86 { | 18 class Rel32FinderWin32X86 { |
| 21 public: | 19 public: |
| 22 Rel32FinderWin32X86(RVA relocs_start_rva, RVA relocs_end_rva, | 20 Rel32FinderWin32X86(RVA relocs_start_rva, RVA relocs_end_rva); |
| 23 RVA image_end_rva); | |
| 24 virtual ~Rel32FinderWin32X86(); | 21 virtual ~Rel32FinderWin32X86(); |
| 25 | 22 |
| 26 // Subsumes rva != kUnassignedRVA. | 23 // Swaps data in |rel32_locations_| with |dest|. |
| 27 bool IsValidRVA(RVA rva) const { return rva < image_end_rva_; } | |
| 28 | |
| 29 // Swaps data in |rel32_locations_| to |dest|. | |
| 30 void SwapRel32Locations(std::vector<RVA>* dest); | 24 void SwapRel32Locations(std::vector<RVA>* dest); |
| 31 | 25 |
| 32 #if COURGETTE_HISTOGRAM_TARGETS | 26 #if COURGETTE_HISTOGRAM_TARGETS |
| 33 // Swaps data in |rel32_target_rvas_| to |dest|. | 27 // Swaps data in |rel32_target_rvas_| with |dest|. |
| 34 void SwapRel32TargetRVAs(std::map<RVA, int>* dest); | 28 void SwapRel32TargetRVAs(std::map<RVA, int>* dest); |
| 35 #endif | 29 #endif |
| 36 | 30 |
| 37 // Scans through [|start_pointer|, |end_pointer|) for rel32 addresses. Seeks | 31 // Scans through [|start_pointer|, |end_pointer|) for rel32 addresses. Seeks |
| 38 // RVAs that satisfy the following: | 32 // RVAs that satisfy the following: |
| 39 // - Do not collide with |abs32_pos| (assumed sorted). | 33 // - Do not overlap with |abs32_locations| (assumed sorted). |
| 40 // - Do not collide with |base_relocation_table|'s RVA range, | 34 // - Do not overlap with [relocs_start_rva, relocs_end_rva). |
| 41 // - Whose targets are in [|start_rva|, |end_rva|). | 35 // - Whose targets are in [|start_rva|, |end_rva|). |
| 42 // The sorted results are written to |rel32_locations_|. | 36 // The sorted results are written to |rel32_locations_|. |
| 43 virtual void Find(const uint8_t* start_pointer, | 37 virtual void Find(const uint8_t* start_pointer, |
| 44 const uint8_t* end_pointer, | 38 const uint8_t* end_pointer, |
| 45 RVA start_rva, | 39 RVA start_rva, |
| 46 RVA end_rva, | 40 RVA end_rva, |
| 47 const std::vector<RVA>& abs32_locations) = 0; | 41 const std::vector<RVA>& abs32_locations) = 0; |
| 48 | 42 |
| 49 protected: | 43 protected: |
| 50 const RVA relocs_start_rva_; | 44 const RVA relocs_start_rva_; |
| 51 const RVA relocs_end_rva_; | 45 const RVA relocs_end_rva_; |
| 52 const RVA image_end_rva_; | |
| 53 | 46 |
| 54 std::vector<RVA> rel32_locations_; | 47 std::vector<RVA> rel32_locations_; |
| 55 | 48 |
| 56 #if COURGETTE_HISTOGRAM_TARGETS | 49 #if COURGETTE_HISTOGRAM_TARGETS |
| 57 std::map<RVA, int> rel32_target_rvas_; | 50 std::map<RVA, int> rel32_target_rvas_; |
| 58 #endif | 51 #endif |
| 59 }; | 52 }; |
| 60 | 53 |
| 61 // The basic implementation performs naive scan for rel32 JMP and Jcc opcodes | 54 // The basic implementation performs naive scan for rel32 JMP and Jcc opcodes |
| 62 // (excluding JPO/JPE) disregarding instruction alignment. | 55 // (excluding JPO/JPE) disregarding instruction alignment. |
| 63 class Rel32FinderWin32X86_Basic : public Rel32FinderWin32X86 { | 56 class Rel32FinderWin32X86_Basic : public Rel32FinderWin32X86 { |
| 64 public: | 57 public: |
| 65 Rel32FinderWin32X86_Basic(RVA relocs_start_rva, RVA relocs_end_rva, | 58 // |image_end_rva| is used to heuristically check target RVA validity. |
| 66 RVA image_end_rva); | 59 Rel32FinderWin32X86_Basic( |
| 60 RVA relocs_start_rva, RVA relocs_end_rva, RVA image_end_rva); |
| 67 virtual ~Rel32FinderWin32X86_Basic(); | 61 virtual ~Rel32FinderWin32X86_Basic(); |
| 68 | 62 |
| 69 // Rel32FinderWin32X86 implementation. | 63 // Rel32FinderWin32X86 implementation. |
| 70 void Find(const uint8_t* start_pointer, | 64 void Find(const uint8_t* start_pointer, |
| 71 const uint8_t* end_pointer, | 65 const uint8_t* end_pointer, |
| 72 RVA start_rva, | 66 RVA start_rva, |
| 73 RVA end_rva, | 67 RVA end_rva, |
| 74 const std::vector<RVA>& abs32_locations) override; | 68 const std::vector<RVA>& abs32_locations) override; |
| 69 |
| 70 protected: |
| 71 const RVA image_end_rva_; |
| 75 }; | 72 }; |
| 76 | 73 |
| 77 } // namespace courgette | 74 } // namespace courgette |
| 78 | 75 |
| 79 #endif // COURGETTE_REL32_FINDER_WIN32_X86_H_ | 76 #endif // COURGETTE_REL32_FINDER_WIN32_X86_H_ |
| OLD | NEW |