Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / courgette / disassembler_win32_x64.cc
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.
4
5 #include "courgette/disassembler_win32_x64.h"
6
7 #include <algorithm>
8
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"
14
15 #if COURGETTE_HISTOGRAM_TARGETS
16 #include <iostream>
17 #endif
18
19 namespace courgette {
20
21 DisassemblerWin32X64::DisassemblerWin32X64(const uint8_t* start, size_t length)
22     : DisassemblerWin32(start, length) {}
23
24 RVA DisassemblerWin32X64::PointerToTargetRVA(const uint8_t* p) const {
25   return Address64ToRVA(Read64LittleEndian(p));
26 }
27
28 RVA DisassemblerWin32X64::Address64ToRVA(uint64_t address) const {
29   if (address < image_base() || address >= image_base() + size_of_image_)
30     return kNoRVA;
31   return base::checked_cast<RVA>(address - image_base());
32 }
33
34 CheckBool DisassemblerWin32X64::EmitAbs(Label* label,
35                                         InstructionReceptor* receptor) const {
36   return receptor->EmitAbs64(label);
37 }
38
39 void DisassemblerWin32X64::ParseRel32RelocsFromSection(const Section* section) {
40   // TODO(sra): use characteristic.
41   bool isCode = strcmp(section->name, ".text") == 0;
42   if (!isCode)
43     return;
44
45   FileOffset start_file_offset = section->file_offset_of_raw_data;
46   FileOffset end_file_offset = start_file_offset + section->size_of_raw_data;
47
48   const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
49   const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
50
51   RVA start_rva = FileOffsetToRVA(start_file_offset);
52   RVA end_rva = start_rva + section->virtual_size;
53
54   Rel32FinderX64 finder(
55       base_relocation_table().address_,
56       base_relocation_table().address_ + base_relocation_table().size_,
57       size_of_image_);
58   finder.Find(start_pointer, end_pointer, start_rva, end_rva, abs32_locations_);
59   finder.SwapRel32Locations(&rel32_locations_);
60
61 #if COURGETTE_HISTOGRAM_TARGETS
62   DCHECK(rel32_target_rvas_.empty());
63   finder.SwapRel32TargetRVAs(&rel32_target_rvas_);
64 #endif
65 }
66
67 }  // namespace courgette