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.
5 #include "courgette/disassembler_win32_x64.h"
9 #include "base/logging.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 FileOffset end_file_offset = start_file_offset + section->size_of_raw_data;
48 const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
49 const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
51 RVA start_rva = FileOffsetToRVA(start_file_offset);
52 RVA end_rva = start_rva + section->virtual_size;
54 Rel32FinderX64 finder(
55 base_relocation_table().address_,
56 base_relocation_table().address_ + base_relocation_table().size_,
58 finder.Find(start_pointer, end_pointer, start_rva, end_rva, abs32_locations_);
59 finder.SwapRel32Locations(&rel32_locations_);
61 #if COURGETTE_HISTOGRAM_TARGETS
62 DCHECK(rel32_target_rvas_.empty());
63 finder.SwapRel32TargetRVAs(&rel32_target_rvas_);
67 } // namespace courgette