Chromium Code Reviews| Index: courgette/rel32_finder_x64.cc | 
| diff --git a/courgette/disassembler_win32_x64.cc b/courgette/rel32_finder_x64.cc | 
| similarity index 52% | 
| copy from courgette/disassembler_win32_x64.cc | 
| copy to courgette/rel32_finder_x64.cc | 
| index c4d89e452e62875fe402af773f60110710691236..080d68f59d1f02867b0b6167b7c8a834a73a0f96 100644 | 
| --- a/courgette/disassembler_win32_x64.cc | 
| +++ b/courgette/rel32_finder_x64.cc | 
| @@ -1,72 +1,37 @@ | 
| -// Copyright 2013 The Chromium Authors. All rights reserved. | 
| +// Copyright 2016 The Chromium Authors. All rights reserved. | 
| // Use of this source code is governed by a BSD-style license that can be | 
| // found in the LICENSE file. | 
| -#include "courgette/disassembler_win32_x64.h" | 
| - | 
| -#include <algorithm> | 
| - | 
| -#include "base/logging.h" | 
| -#include "base/numerics/safe_conversions.h" | 
| -#include "courgette/assembly_program.h" | 
| -#include "courgette/courgette.h" | 
| - | 
| -#if COURGETTE_HISTOGRAM_TARGETS | 
| -#include <iostream> | 
| -#endif | 
| +#include "courgette/rel32_finder_x64.h" | 
| namespace courgette { | 
| -DisassemblerWin32X64::DisassemblerWin32X64(const void* start, size_t length) | 
| - : DisassemblerWin32(start, length) {} | 
| - | 
| -RVA DisassemblerWin32X64::PointerToTargetRVA(const uint8_t* p) const { | 
| - return Address64ToRVA(Read64LittleEndian(p)); | 
| -} | 
| - | 
| -RVA DisassemblerWin32X64::Address64ToRVA(uint64_t address) const { | 
| - if (address < image_base() || address >= image_base() + size_of_image_) | 
| - return kNoRVA; | 
| - return base::checked_cast<RVA>(address - image_base()); | 
| -} | 
| - | 
| -CheckBool DisassemblerWin32X64::EmitAbs(Label* label, | 
| - AssemblyProgram* program) { | 
| - return program->EmitAbs64(label); | 
| -} | 
| - | 
| -void DisassemblerWin32X64::ParseRel32RelocsFromSection(const Section* section) { | 
| - // TODO(sra): use characteristic. | 
| - bool isCode = strcmp(section->name, ".text") == 0; | 
| - if (!isCode) | 
| - return; | 
| - | 
| - 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); | 
| - | 
| - RVA start_rva = FileOffsetToRVA(start_file_offset); | 
| - RVA end_rva = start_rva + section->virtual_size; | 
| +Rel32FinderX64::Rel32FinderX64(RVA relocs_start_rva, RVA relocs_end_rva) | 
| + : Rel32Finder(relocs_start_rva, relocs_end_rva) {} | 
| +void Rel32FinderX64::Find(const uint8_t* start_pointer, | 
| + const uint8_t* end_pointer, | 
| + RVA start_rva, | 
| + RVA end_rva, | 
| + uint32_t size_of_image, | 
| + const std::vector<RVA>& abs32_locations) { | 
| // Quick way to convert from Pointer to RVA within a single Section is to | 
| - // subtract |pointer_to_rva|. | 
| + // subtract 'pointer_to_rva'. | 
| 
 
huangs
2016/05/26 22:11:17
NIT: |adjust_pointer_to_rva|.
 
etiennep
2016/05/27 18:12:34
Done.
 
 | 
| const uint8_t* const adjust_pointer_to_rva = start_pointer - start_rva; | 
| - std::vector<RVA>::iterator abs32_pos = abs32_locations_.begin(); | 
| + std::vector<RVA>::const_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; | 
| - } | 
| + | 
| + // Skip the base reloation table if we encounter it. | 
| + // Note: We're not bothering to handle the edge case where a Rel32 pointer | 
| + // collides with |relocs_start_rva_| by being {1, 2, 3}-bytes before it. | 
| + if (current_rva >= relocs_start_rva_ && current_rva < relocs_end_rva_) { | 
| + p += relocs_end_rva_ - current_rva; | 
| + continue; | 
| } | 
| // Heuristic discovery of rel32 locations in instruction stream: are the | 
| @@ -81,7 +46,7 @@ void DisassemblerWin32X64::ParseRel32RelocsFromSection(const Section* section) { | 
| } | 
| 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 | 
| + 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 | 
| @@ -107,24 +72,23 @@ void DisassemblerWin32X64::ParseRel32RelocsFromSection(const Section* section) { | 
| 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) | 
| + 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 != 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. | 
| + // overlap. Skip four bytes past the abs32 reloc. | 
| 
 
huangs
2016/05/26 22:11:17
NIT: Remove space before "Skip".
 
etiennep
2016/05/27 18:12:34
Done.
 
 | 
| p += (*abs32_pos + 4) - current_rva; | 
| continue; | 
| } | 
| } | 
| 
 
huangs
2016/05/26 22:40:55
Why did the comment disappear?
 
etiennep
2016/05/27 18:12:34
Idk, it's back now!
 
 | 
| - // + 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. | 
| + if (target_rva < size_of_image && // Subsumes rva != kUnassignedRVA. | 
| 
 
huangs
2016/05/26 22:40:55
target_rva < size_of_image
seems kinda bogus and
 
etiennep
2016/05/27 18:12:34
I moved size_of_image as an member variable. It wo
 
 | 
| (is_rip_relative || | 
| (start_rva <= target_rva && target_rva < end_rva))) { | 
| rel32_locations_.push_back(rel32_rva); |