disassembler_win32.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2017 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 COMPONENTS_ZUCCHINI_DISASSEMBLER_WIN32_H_
  5. #define COMPONENTS_ZUCCHINI_DISASSEMBLER_WIN32_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <deque>
  9. #include <memory>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. #include "base/memory/raw_ptr.h"
  14. #include "components/zucchini/address_translator.h"
  15. #include "components/zucchini/buffer_view.h"
  16. #include "components/zucchini/disassembler.h"
  17. #include "components/zucchini/image_utils.h"
  18. #include "components/zucchini/type_win_pe.h"
  19. namespace zucchini {
  20. class Rel32FinderX86;
  21. class Rel32FinderX64;
  22. struct Win32X86Traits {
  23. static constexpr uint16_t kVersion = 1;
  24. static constexpr Bitness kBitness = kBit32;
  25. static constexpr ExecutableType kExeType = kExeTypeWin32X86;
  26. enum : uint16_t { kMagic = 0x10B };
  27. enum : uint16_t { kRelocType = 3 };
  28. enum : uint32_t { kVAWidth = 4 };
  29. static const char kExeTypeString[];
  30. using ImageOptionalHeader = pe::ImageOptionalHeader;
  31. using RelFinder = Rel32FinderX86;
  32. using Address = uint32_t;
  33. };
  34. struct Win32X64Traits {
  35. static constexpr uint16_t kVersion = 1;
  36. static constexpr Bitness kBitness = kBit64;
  37. static constexpr ExecutableType kExeType = kExeTypeWin32X64;
  38. enum : uint16_t { kMagic = 0x20B };
  39. enum : uint16_t { kRelocType = 10 };
  40. enum : uint32_t { kVAWidth = 8 };
  41. static const char kExeTypeString[];
  42. using ImageOptionalHeader = pe::ImageOptionalHeader64;
  43. using RelFinder = Rel32FinderX64;
  44. using Address = uint64_t;
  45. };
  46. template <class TRAITS>
  47. class DisassemblerWin32 : public Disassembler {
  48. public:
  49. using Traits = TRAITS;
  50. static constexpr uint16_t kVersion = Traits::kVersion;
  51. enum ReferenceType : uint8_t { kReloc, kAbs32, kRel32, kTypeCount };
  52. // Applies quick checks to determine whether |image| *may* point to the start
  53. // of an executable. Returns true iff the check passes.
  54. static bool QuickDetect(ConstBufferView image);
  55. DisassemblerWin32();
  56. DisassemblerWin32(const DisassemblerWin32&) = delete;
  57. const DisassemblerWin32& operator=(const DisassemblerWin32&) = delete;
  58. ~DisassemblerWin32() override;
  59. // Disassembler:
  60. ExecutableType GetExeType() const override;
  61. std::string GetExeTypeString() const override;
  62. std::vector<ReferenceGroup> MakeReferenceGroups() const override;
  63. // Functions that return reader / writer for references.
  64. std::unique_ptr<ReferenceReader> MakeReadRelocs(offset_t lo, offset_t hi);
  65. std::unique_ptr<ReferenceReader> MakeReadAbs32(offset_t lo, offset_t hi);
  66. std::unique_ptr<ReferenceReader> MakeReadRel32(offset_t lo, offset_t hi);
  67. std::unique_ptr<ReferenceWriter> MakeWriteRelocs(MutableBufferView image);
  68. std::unique_ptr<ReferenceWriter> MakeWriteAbs32(MutableBufferView image);
  69. std::unique_ptr<ReferenceWriter> MakeWriteRel32(MutableBufferView image);
  70. private:
  71. friend Disassembler;
  72. // Disassembler:
  73. bool Parse(ConstBufferView image) override;
  74. // Parses the file header. Returns true iff successful.
  75. bool ParseHeader();
  76. // Parsers to extract references. These are lazily called, and return whether
  77. // parsing was successful (failures are non-fatal).
  78. bool ParseAndStoreRelocBlocks();
  79. bool ParseAndStoreAbs32();
  80. bool ParseAndStoreRel32();
  81. // In-memory copy of sections.
  82. std::vector<pe::ImageSectionHeader> sections_;
  83. // Image base address to translate between RVA and VA.
  84. typename Traits::Address image_base_ = 0;
  85. // Pointer to data Directory entry of the relocation table.
  86. raw_ptr<const pe::ImageDataDirectory> base_relocation_table_ = nullptr;
  87. // Translator between offsets and RVAs.
  88. AddressTranslator translator_;
  89. // Reference storage.
  90. BufferRegion reloc_region_ = {kInvalidOffset, 0U};
  91. std::vector<offset_t> reloc_block_offsets_;
  92. offset_t reloc_end_ = 0;
  93. std::deque<offset_t> abs32_locations_;
  94. // Using std::deque to reduce peak memory footprint.
  95. std::deque<offset_t> rel32_locations_;
  96. // Initialization states of reference storage, used for lazy initialization.
  97. // TODO(huangs): Investigate whether lazy initialization is useful for memory
  98. // reduction. This is a carryover from Courgette. To be sure we should run
  99. // experiment after Zucchini is able to do ensemble patching.
  100. bool has_parsed_relocs_ = false;
  101. bool has_parsed_abs32_ = false;
  102. bool has_parsed_rel32_ = false;
  103. };
  104. using DisassemblerWin32X86 = DisassemblerWin32<Win32X86Traits>;
  105. using DisassemblerWin32X64 = DisassemblerWin32<Win32X64Traits>;
  106. } // namespace zucchini
  107. #endif // COMPONENTS_ZUCCHINI_DISASSEMBLER_WIN32_H_