disassembler_win32_x86.cc 2.3 KB

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