1 // Copyright 2011 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_x86.h"
9 #include "base/check.h"
10 #include "courgette/assembly_program.h"
11 #include "courgette/courgette.h"
12 #include "courgette/rel32_finder_x86.h"
14 #if COURGETTE_HISTOGRAM_TARGETS
20 DisassemblerWin32X86::DisassemblerWin32X86(const uint8_t* start, size_t length)
21 : DisassemblerWin32(start, length) {}
23 RVA DisassemblerWin32X86::PointerToTargetRVA(const uint8_t* p) const {
24 return Address32ToRVA(Read32LittleEndian(p));
27 RVA DisassemblerWin32X86::Address32ToRVA(uint32_t address) const {
28 if (address < image_base() || address >= image_base() + size_of_image_)
30 return static_cast<RVA>(address - image_base());
33 CheckBool DisassemblerWin32X86::EmitAbs(Label* label,
34 InstructionReceptor* receptor) const {
35 return receptor->EmitAbs32(label);
38 void DisassemblerWin32X86::ParseRel32RelocsFromSection(const Section* section) {
39 // TODO(sra): use characteristic.
40 bool isCode = strcmp(section->name, ".text") == 0;
44 FileOffset start_file_offset = section->file_offset_of_raw_data;
45 // |virtual_size < size_of_raw_data| is possible. In this case, disassembly
46 // should not proceed beyond |virtual_size|, so rel32 location RVAs remain
47 // translatable to file offsets.
48 FileOffset end_file_offset =
50 std::min(section->virtual_size, section->size_of_raw_data);
52 const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
53 const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
55 RVA start_rva = FileOffsetToRVA(start_file_offset);
56 RVA end_rva = start_rva + section->virtual_size;
58 Rel32FinderX86 finder(
59 base_relocation_table().address_,
60 base_relocation_table().address_ + base_relocation_table().size_);
61 finder.Find(start_pointer, end_pointer, start_rva, end_rva, abs32_locations_);
62 finder.SwapRel32Locations(&rel32_locations_);
64 #if COURGETTE_HISTOGRAM_TARGETS
65 DCHECK(rel32_target_rvas_.empty());
66 finder.SwapRel32TargetRVAs(&rel32_target_rvas_);
70 } // namespace courgette