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