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