1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "courgette/disassembler_win32_x64.h"
9 #include "base/check.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "courgette/assembly_program.h"
12 #include "courgette/courgette.h"
13 #include "courgette/rel32_finder_x64.h"
15 #if COURGETTE_HISTOGRAM_TARGETS
21 DisassemblerWin32X64::DisassemblerWin32X64(const uint8_t* start, size_t length)
22 : DisassemblerWin32(start, length) {}
24 RVA DisassemblerWin32X64::PointerToTargetRVA(const uint8_t* p) const {
25 return Address64ToRVA(Read64LittleEndian(p));
28 RVA DisassemblerWin32X64::Address64ToRVA(uint64_t address) const {
29 if (address < image_base() || address >= image_base() + size_of_image_)
31 return base::checked_cast<RVA>(address - image_base());
34 CheckBool DisassemblerWin32X64::EmitAbs(Label* label,
35 InstructionReceptor* receptor) const {
36 return receptor->EmitAbs64(label);
39 void DisassemblerWin32X64::ParseRel32RelocsFromSection(const Section* section) {
40 // TODO(sra): use characteristic.
41 bool isCode = strcmp(section->name, ".text") == 0;
45 FileOffset start_file_offset = section->file_offset_of_raw_data;
46 // |virtual_size < size_of_raw_data| is possible. In this case, disassembly
47 // should not proceed beyond |virtual_size|, so rel32 location RVAs remain
48 // translatable to file offsets.
49 FileOffset end_file_offset =
51 std::min(section->virtual_size, section->size_of_raw_data);
53 const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
54 const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
56 RVA start_rva = FileOffsetToRVA(start_file_offset);
57 RVA end_rva = start_rva + section->virtual_size;
59 Rel32FinderX64 finder(
60 base_relocation_table().address_,
61 base_relocation_table().address_ + base_relocation_table().size_,
63 finder.Find(start_pointer, end_pointer, start_rva, end_rva, abs32_locations_);
64 finder.SwapRel32Locations(&rel32_locations_);
66 #if COURGETTE_HISTOGRAM_TARGETS
67 DCHECK(rel32_target_rvas_.empty());
68 finder.SwapRel32TargetRVAs(&rel32_target_rvas_);
72 } // namespace courgette