disassembler.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (c) 2011 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_DISASSEMBLER_H_
  5. #define COURGETTE_DISASSEMBLER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "courgette/courgette.h"
  10. #include "courgette/image_utils.h"
  11. #include "courgette/instruction_utils.h"
  12. namespace courgette {
  13. class AssemblyProgram;
  14. class EncodedProgram;
  15. class Disassembler : public AddressTranslator {
  16. public:
  17. // Visitor/adaptor to translate RVA to target RVA for abs32.
  18. class RvaVisitor_Abs32 : public VectorRvaVisitor<RVA> {
  19. public:
  20. RvaVisitor_Abs32(const std::vector<RVA>& rva_locations,
  21. const AddressTranslator& translator);
  22. RvaVisitor_Abs32(const RvaVisitor_Abs32&) = delete;
  23. RvaVisitor_Abs32& operator=(const RvaVisitor_Abs32&) = delete;
  24. ~RvaVisitor_Abs32() override { }
  25. // VectorRvaVisitor<RVA> interfaces.
  26. RVA Get() const override;
  27. private:
  28. const AddressTranslator& translator_;
  29. };
  30. // Visitor/adaptor to translate RVA to target RVA for rel32.
  31. class RvaVisitor_Rel32 : public VectorRvaVisitor<RVA> {
  32. public:
  33. RvaVisitor_Rel32(const std::vector<RVA>& rva_locations,
  34. const AddressTranslator& translator);
  35. RvaVisitor_Rel32(const RvaVisitor_Rel32&) = delete;
  36. RvaVisitor_Rel32& operator=(const RvaVisitor_Rel32&) = delete;
  37. ~RvaVisitor_Rel32() override { }
  38. // VectorRvaVisitor<RVA> interfaces.
  39. RVA Get() const override;
  40. private:
  41. const AddressTranslator& translator_;
  42. };
  43. Disassembler(const Disassembler&) = delete;
  44. Disassembler& operator=(const Disassembler&) = delete;
  45. virtual ~Disassembler();
  46. // AddressTranslator interfaces.
  47. RVA FileOffsetToRVA(FileOffset file_offset) const override = 0;
  48. FileOffset RVAToFileOffset(RVA rva) const override = 0;
  49. const uint8_t* FileOffsetToPointer(FileOffset file_offset) const override;
  50. const uint8_t* RVAToPointer(RVA rva) const override;
  51. RVA PointerToTargetRVA(const uint8_t* p) const override = 0;
  52. virtual ExecutableType kind() const = 0;
  53. // Returns the preferred image base address. Using uint64_t to accommodate the
  54. // general case of 64-bit architectures.
  55. virtual uint64_t image_base() const = 0;
  56. // Extracts and stores locations of abs32 references from the image file.
  57. virtual bool ExtractAbs32Locations() = 0;
  58. // Extracts and stores locations of rel32 references from the image file.
  59. virtual bool ExtractRel32Locations() = 0;
  60. // Returns a caller-owned new RvaVisitor to iterate through abs32 target RVAs.
  61. virtual RvaVisitor* CreateAbs32TargetRvaVisitor() = 0;
  62. // Returns a caller-owned new RvaVisitor to iterate through rel32 target RVAs.
  63. virtual RvaVisitor* CreateRel32TargetRvaVisitor() = 0;
  64. // Removes unused rel32 locations (architecture-specific). This is needed
  65. // because we may remove rel32 Labels along the way. As a result the matching
  66. // rel32 addresses become unused. Removing them saves space.
  67. virtual void RemoveUnusedRel32Locations(AssemblyProgram* program) = 0;
  68. // Extracts structural data from the main image. Returns true if the image
  69. // appears to be a valid executable of the expected type, or false otherwise.
  70. // This needs to be called before Disassemble().
  71. virtual bool ParseHeader() = 0;
  72. // Extracts and stores references from the main image. Returns a new
  73. // AssemblyProgram with initialized Labels, or null on failure.
  74. std::unique_ptr<AssemblyProgram> CreateProgram(bool annotate);
  75. // Goes through the entire program (with the help of |program|), computes all
  76. // instructions, and stores them into |encoded|.
  77. Status DisassembleAndEncode(AssemblyProgram* program,
  78. EncodedProgram* encoded);
  79. // ok() may always be called but returns true only after ParseHeader()
  80. // succeeds.
  81. bool ok() const { return failure_reason_ == nullptr; }
  82. // Returns the length of the image. May reduce after ParseHeader().
  83. size_t length() const { return length_; }
  84. const uint8_t* start() const { return start_; }
  85. const uint8_t* end() const { return end_; }
  86. protected:
  87. Disassembler(const uint8_t* start, size_t length);
  88. bool Good();
  89. bool Bad(const char *reason);
  90. // Returns true if the given range lies within our memory region.
  91. bool IsRangeInBounds(size_t offset, size_t size) {
  92. return offset <= length() && size <= length() - offset;
  93. }
  94. // Returns true if the given array lies within our memory region.
  95. bool IsArrayInBounds(size_t offset, size_t elements, size_t element_size) {
  96. return offset <= length() && elements <= (length() - offset) / element_size;
  97. }
  98. // Computes and stores all Labels before scanning program bytes.
  99. void PrecomputeLabels(AssemblyProgram* program);
  100. // Reduce the length of the image in memory. Does not actually free
  101. // (or realloc) any memory. Usually only called via ParseHeader().
  102. void ReduceLength(size_t reduced_length);
  103. // Returns a generator that emits instructions to a given receptor. |program|
  104. // is required as helper.
  105. virtual InstructionGenerator GetInstructionGenerator(
  106. AssemblyProgram* program) = 0;
  107. private:
  108. const char* failure_reason_;
  109. //
  110. // Basic information that is always valid after construction, although
  111. // ParseHeader() may shorten |length_| if the executable is shorter than the
  112. // total data.
  113. //
  114. size_t length_; // In current memory.
  115. const uint8_t* start_; // In current memory, base for 'file offsets'.
  116. const uint8_t* end_; // In current memory.
  117. };
  118. } // namespace courgette
  119. #endif // COURGETTE_DISASSEMBLER_H_