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