[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 1 | // Copyright (c) 2011 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_x86.h" | ||||
6 | |||||
7 | #include <algorithm> | ||||
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 8 | |
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 10 | #include "courgette/assembly_program.h" |
11 | #include "courgette/courgette.h" | ||||
etiennep | 7a43c3e | 2016-06-07 17:40:38 | [diff] [blame] | 12 | #include "courgette/rel32_finder_x86.h" |
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 13 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 14 | #if COURGETTE_HISTOGRAM_TARGETS |
15 | #include <iostream> | ||||
16 | #endif | ||||
17 | |||||
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 18 | namespace courgette { |
19 | |||||
etiennep | 5059bca | 2016-07-08 17:55:20 | [diff] [blame] | 20 | DisassemblerWin32X86::DisassemblerWin32X86(const uint8_t* start, size_t length) |
etiennep | 5ac34c9 | 2016-05-20 15:44:46 | [diff] [blame] | 21 | : DisassemblerWin32(start, length) {} |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 22 | |
huangs | f940a8c9 | 2016-03-23 20:40:35 | [diff] [blame] | 23 | RVA DisassemblerWin32X86::PointerToTargetRVA(const uint8_t* p) const { |
24 | return Address32ToRVA(Read32LittleEndian(p)); | ||||
[email protected] | 423a381 | 2011-10-26 00:50:20 | [diff] [blame] | 25 | } |
26 | |||||
huangs | f940a8c9 | 2016-03-23 20:40:35 | [diff] [blame] | 27 | RVA DisassemblerWin32X86::Address32ToRVA(uint32_t address) const { |
28 | if (address < image_base() || address >= image_base() + size_of_image_) | ||||
29 | return kNoRVA; | ||||
30 | return static_cast<RVA>(address - image_base()); | ||||
31 | } | ||||
32 | |||||
etiennep | 5ac34c9 | 2016-05-20 15:44:46 | [diff] [blame] | 33 | CheckBool DisassemblerWin32X86::EmitAbs(Label* label, |
huangs | 7b221a5 | 2016-11-09 22:28:23 | [diff] [blame] | 34 | InstructionReceptor* receptor) const { |
35 | return receptor->EmitAbs32(label); | ||||
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 36 | } |
37 | |||||
38 | void DisassemblerWin32X86::ParseRel32RelocsFromSection(const Section* section) { | ||||
39 | // TODO(sra): use characteristic. | ||||
40 | bool isCode = strcmp(section->name, ".text") == 0; | ||||
41 | if (!isCode) | ||||
42 | return; | ||||
43 | |||||
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 44 | FileOffset start_file_offset = section->file_offset_of_raw_data; |
45 | FileOffset end_file_offset = start_file_offset + section->size_of_raw_data; | ||||
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 46 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 47 | const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset); |
48 | const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset); | ||||
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 49 | |
[email protected] | 423a381 | 2011-10-26 00:50:20 | [diff] [blame] | 50 | RVA start_rva = FileOffsetToRVA(start_file_offset); |
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 51 | RVA end_rva = start_rva + section->virtual_size; |
52 | |||||
etiennep | 7a43c3e | 2016-06-07 17:40:38 | [diff] [blame] | 53 | Rel32FinderX86 finder( |
huangs | 6d2a303 | 2015-09-18 18:52:56 | [diff] [blame] | 54 | base_relocation_table().address_, |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 55 | base_relocation_table().address_ + base_relocation_table().size_); |
huangs | 6d2a303 | 2015-09-18 18:52:56 | [diff] [blame] | 56 | finder.Find(start_pointer, end_pointer, start_rva, end_rva, abs32_locations_); |
57 | finder.SwapRel32Locations(&rel32_locations_); | ||||
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 58 | |
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 59 | #if COURGETTE_HISTOGRAM_TARGETS |
huangs | 6d2a303 | 2015-09-18 18:52:56 | [diff] [blame] | 60 | DCHECK(rel32_target_rvas_.empty()); |
61 | finder.SwapRel32TargetRVAs(&rel32_target_rvas_); | ||||
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 62 | #endif |
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 63 | } |
64 | |||||
[email protected] | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 65 | } // namespace courgette |