rel32_finder.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2016 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.
  4. #ifndef COURGETTE_REL32_FINDER_H_
  5. #define COURGETTE_REL32_FINDER_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <vector>
  9. #include "courgette/image_utils.h"
  10. namespace courgette {
  11. // A helper class to scan through a section of code to extract RVAs.
  12. class Rel32Finder {
  13. public:
  14. Rel32Finder(RVA relocs_start_rva, RVA relocs_end_rva);
  15. virtual ~Rel32Finder() = default;
  16. // Swaps data in |rel32_locations_| with |dest|.
  17. void SwapRel32Locations(std::vector<RVA>* dest);
  18. #if COURGETTE_HISTOGRAM_TARGETS
  19. // Swaps data in |rel32_target_rvas_| with |dest|.
  20. void SwapRel32TargetRVAs(std::map<RVA, int>* dest);
  21. #endif
  22. // Scans through [|start_pointer|, |end_pointer|) for rel32 addresses. Seeks
  23. // RVAs that satisfy the following:
  24. // - Do not overlap with |abs32_locations| (assumed sorted).
  25. // - Do not overlap with [relocs_start_rva, relocs_end_rva).
  26. // - Whose targets are in [|start_rva|, |end_rva|).
  27. // The sorted results are written to |rel32_locations_|.
  28. virtual void Find(const uint8_t* start_pointer,
  29. const uint8_t* end_pointer,
  30. RVA start_rva,
  31. RVA end_rva,
  32. const std::vector<RVA>& abs32_locations) = 0;
  33. protected:
  34. const RVA relocs_start_rva_;
  35. const RVA relocs_end_rva_;
  36. std::vector<RVA> rel32_locations_;
  37. #if COURGETTE_HISTOGRAM_TARGETS
  38. std::map<RVA, int> rel32_target_rvas_;
  39. #endif
  40. };
  41. } // namespace courgette
  42. #endif // COURGETTE_REL32_FINDER_H_