| Index: courgette/disassembler_win32_x64.cc
|
| diff --git a/courgette/disassembler_win32_x64.cc b/courgette/disassembler_win32_x64.cc
|
| index c4d89e452e62875fe402af773f60110710691236..d4ed28cd24b8080c311214ab5cffebe7c16365bf 100644
|
| --- a/courgette/disassembler_win32_x64.cc
|
| +++ b/courgette/disassembler_win32_x64.cc
|
| @@ -10,6 +10,7 @@
|
| #include "base/numerics/safe_conversions.h"
|
| #include "courgette/assembly_program.h"
|
| #include "courgette/courgette.h"
|
| +#include "courgette/rel32_finder_x64.h"
|
|
|
| #if COURGETTE_HISTOGRAM_TARGETS
|
| #include <iostream>
|
| @@ -43,7 +44,6 @@ void DisassemblerWin32X64::ParseRel32RelocsFromSection(const Section* section) {
|
|
|
| FileOffset start_file_offset = section->file_offset_of_raw_data;
|
| FileOffset end_file_offset = start_file_offset + section->size_of_raw_data;
|
| - RVA relocs_start_rva = base_relocation_table().address_;
|
|
|
| const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
|
| const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
|
| @@ -51,92 +51,17 @@ void DisassemblerWin32X64::ParseRel32RelocsFromSection(const Section* section) {
|
| RVA start_rva = FileOffsetToRVA(start_file_offset);
|
| RVA end_rva = start_rva + section->virtual_size;
|
|
|
| - // Quick way to convert from Pointer to RVA within a single Section is to
|
| - // subtract |pointer_to_rva|.
|
| - const uint8_t* const adjust_pointer_to_rva = start_pointer - start_rva;
|
| -
|
| - std::vector<RVA>::iterator abs32_pos = abs32_locations_.begin();
|
| -
|
| - // Find the rel32 relocations.
|
| - const uint8_t* p = start_pointer;
|
| - while (p < end_pointer) {
|
| - RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva);
|
| - if (current_rva == relocs_start_rva) {
|
| - uint32_t relocs_size = base_relocation_table().size_;
|
| - if (relocs_size) {
|
| - p += relocs_size;
|
| - continue;
|
| - }
|
| - }
|
| -
|
| - // Heuristic discovery of rel32 locations in instruction stream: are the
|
| - // next few bytes the start of an instruction containing a rel32
|
| - // addressing mode?
|
| - const uint8_t* rel32 = nullptr;
|
| - bool is_rip_relative = false;
|
| -
|
| - if (p + 5 <= end_pointer) {
|
| - if (*p == 0xE8 || *p == 0xE9) // jmp rel32 and call rel32
|
| - rel32 = p + 1;
|
| - }
|
| - if (p + 6 <= end_pointer) {
|
| - if (*p == 0x0F && (*(p + 1) & 0xF0) == 0x80) { // Jcc long form
|
| - if (p[1] != 0x8A && p[1] != 0x8B) // JPE/JPO unlikely
|
| - rel32 = p + 2;
|
| - } else if (*p == 0xFF && (*(p + 1) == 0x15 || *(p + 1) == 0x25)) {
|
| - // rip relative call/jmp
|
| - rel32 = p + 2;
|
| - is_rip_relative = true;
|
| - }
|
| - }
|
| - if (p + 7 <= end_pointer) {
|
| - if ((*p & 0xFB) == 0x48 && *(p + 1) == 0x8D &&
|
| - (*(p + 2) & 0xC7) == 0x05) {
|
| - // rip relative lea
|
| - rel32 = p + 3;
|
| - is_rip_relative = true;
|
| - } else if ((*p & 0xFB) == 0x48 && *(p + 1) == 0x8B &&
|
| - (*(p + 2) & 0xC7) == 0x05) {
|
| - // rip relative mov
|
| - rel32 = p + 3;
|
| - is_rip_relative = true;
|
| - }
|
| - }
|
| -
|
| - if (rel32) {
|
| - RVA rel32_rva = static_cast<RVA>(rel32 - adjust_pointer_to_rva);
|
| -
|
| - // Is there an abs32 reloc overlapping the candidate?
|
| - while (abs32_pos != abs32_locations_.end() && *abs32_pos < rel32_rva - 3)
|
| - ++abs32_pos;
|
| - // Now: (*abs32_pos > rel32_rva - 4) i.e. the lowest addressed 4-byte
|
| - // region that could overlap rel32_rva.
|
| - if (abs32_pos != abs32_locations_.end()) {
|
| - if (*abs32_pos < rel32_rva + 4) {
|
| - // Beginning of abs32 reloc is before end of rel32 reloc so they
|
| - // overlap. Skip four bytes past the abs32 reloc.
|
| - p += (*abs32_pos + 4) - current_rva;
|
| - continue;
|
| - }
|
| - }
|
| -
|
| - // + 4 since offset is relative to start of next instruction.
|
| - RVA target_rva = rel32_rva + 4 + Read32LittleEndian(rel32);
|
| - // To be valid, rel32 target must be within image, and within this
|
| - // section.
|
| - if (target_rva < size_of_image_ && // Subsumes rva != kUnassignedRVA.
|
| - (is_rip_relative ||
|
| - (start_rva <= target_rva && target_rva < end_rva))) {
|
| - rel32_locations_.push_back(rel32_rva);
|
| + Rel32FinderX64 finder(
|
| + base_relocation_table().address_,
|
| + base_relocation_table().address_ + base_relocation_table().size_,
|
| + size_of_image_);
|
| + finder.Find(start_pointer, end_pointer, start_rva, end_rva, abs32_locations_);
|
| + finder.SwapRel32Locations(&rel32_locations_);
|
| +
|
| #if COURGETTE_HISTOGRAM_TARGETS
|
| - ++rel32_target_rvas_[target_rva];
|
| + DCHECK(rel32_target_rvas_.empty());
|
| + finder.SwapRel32TargetRVAs(&rel32_target_rvas_);
|
| #endif
|
| - p = rel32 + 4;
|
| - continue;
|
| - }
|
| - }
|
| - p += 1;
|
| - }
|
| }
|
|
|
| } // namespace courgette
|
|
|