disassembler_win32_x64.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2013 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. #include "courgette/disassembler_win32_x64.h"
  5. #include <algorithm>
  6. #include "base/check.h"
  7. #include "base/numerics/safe_conversions.h"
  8. #include "courgette/assembly_program.h"
  9. #include "courgette/courgette.h"
  10. #include "courgette/rel32_finder_x64.h"
  11. #if COURGETTE_HISTOGRAM_TARGETS
  12. #include <iostream>
  13. #endif
  14. namespace courgette {
  15. DisassemblerWin32X64::DisassemblerWin32X64(const uint8_t* start, size_t length)
  16. : DisassemblerWin32(start, length) {}
  17. RVA DisassemblerWin32X64::PointerToTargetRVA(const uint8_t* p) const {
  18. return Address64ToRVA(Read64LittleEndian(p));
  19. }
  20. RVA DisassemblerWin32X64::Address64ToRVA(uint64_t address) const {
  21. if (address < image_base() || address >= image_base() + size_of_image_)
  22. return kNoRVA;
  23. return base::checked_cast<RVA>(address - image_base());
  24. }
  25. CheckBool DisassemblerWin32X64::EmitAbs(Label* label,
  26. InstructionReceptor* receptor) const {
  27. return receptor->EmitAbs64(label);
  28. }
  29. void DisassemblerWin32X64::ParseRel32RelocsFromSection(const Section* section) {
  30. // TODO(sra): use characteristic.
  31. bool isCode = strcmp(section->name, ".text") == 0;
  32. if (!isCode)
  33. return;
  34. FileOffset start_file_offset = section->file_offset_of_raw_data;
  35. // |virtual_size < size_of_raw_data| is possible. In this case, disassembly
  36. // should not proceed beyond |virtual_size|, so rel32 location RVAs remain
  37. // translatable to file offsets.
  38. FileOffset end_file_offset =
  39. start_file_offset +
  40. std::min(section->virtual_size, section->size_of_raw_data);
  41. const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
  42. const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
  43. RVA start_rva = FileOffsetToRVA(start_file_offset);
  44. RVA end_rva = start_rva + section->virtual_size;
  45. Rel32FinderX64 finder(
  46. base_relocation_table().address_,
  47. base_relocation_table().address_ + base_relocation_table().size_,
  48. size_of_image_);
  49. finder.Find(start_pointer, end_pointer, start_rva, end_rva, abs32_locations_);
  50. finder.SwapRel32Locations(&rel32_locations_);
  51. #if COURGETTE_HISTOGRAM_TARGETS
  52. DCHECK(rel32_target_rvas_.empty());
  53. finder.SwapRel32TargetRVAs(&rel32_target_rvas_);
  54. #endif
  55. }
  56. } // namespace courgette