OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <stdint.h> |
| 6 |
5 #include "courgette/rel32_finder_win32_x86.h" | 7 #include "courgette/rel32_finder_win32_x86.h" |
6 | 8 |
7 namespace courgette { | 9 namespace courgette { |
8 | 10 |
9 Rel32FinderWin32X86::Rel32FinderWin32X86( | 11 Rel32FinderWin32X86::Rel32FinderWin32X86( |
10 RVA relocs_start_rva, RVA relocs_end_rva, RVA image_end_rva) | 12 RVA relocs_start_rva, RVA relocs_end_rva, RVA image_end_rva) |
11 : relocs_start_rva_(relocs_start_rva), | 13 : relocs_start_rva_(relocs_start_rva), |
12 relocs_end_rva_(relocs_end_rva), | 14 relocs_end_rva_(relocs_end_rva), |
13 image_end_rva_(image_end_rva) { | 15 image_end_rva_(image_end_rva) { |
14 } | 16 } |
(...skipping 12 matching lines...) Expand all Loading... |
27 #endif | 29 #endif |
28 | 30 |
29 Rel32FinderWin32X86_Basic::Rel32FinderWin32X86_Basic( | 31 Rel32FinderWin32X86_Basic::Rel32FinderWin32X86_Basic( |
30 RVA relocs_start_rva, RVA relocs_end_rva, RVA image_end_rva) | 32 RVA relocs_start_rva, RVA relocs_end_rva, RVA image_end_rva) |
31 : Rel32FinderWin32X86(relocs_start_rva, relocs_end_rva, image_end_rva) { | 33 : Rel32FinderWin32X86(relocs_start_rva, relocs_end_rva, image_end_rva) { |
32 } | 34 } |
33 | 35 |
34 Rel32FinderWin32X86_Basic::~Rel32FinderWin32X86_Basic() { | 36 Rel32FinderWin32X86_Basic::~Rel32FinderWin32X86_Basic() { |
35 } | 37 } |
36 | 38 |
37 void Rel32FinderWin32X86_Basic::Find( | 39 void Rel32FinderWin32X86_Basic::Find(const uint8_t* start_pointer, |
38 const uint8* start_pointer, | 40 const uint8_t* end_pointer, |
39 const uint8* end_pointer, | 41 RVA start_rva, |
40 RVA start_rva, | 42 RVA end_rva, |
41 RVA end_rva, | 43 const std::vector<RVA>& abs32_locations) { |
42 const std::vector<RVA>& abs32_locations) { | |
43 // Quick way to convert from Pointer to RVA within a single Section is to | 44 // Quick way to convert from Pointer to RVA within a single Section is to |
44 // subtract 'pointer_to_rva'. | 45 // subtract 'pointer_to_rva'. |
45 const uint8* const adjust_pointer_to_rva = start_pointer - start_rva; | 46 const uint8_t* const adjust_pointer_to_rva = start_pointer - start_rva; |
46 | 47 |
47 std::vector<RVA>::const_iterator abs32_pos = abs32_locations.begin(); | 48 std::vector<RVA>::const_iterator abs32_pos = abs32_locations.begin(); |
48 | 49 |
49 // Find the rel32 relocations. | 50 // Find the rel32 relocations. |
50 const uint8* p = start_pointer; | 51 const uint8_t* p = start_pointer; |
51 while (p < end_pointer) { | 52 while (p < end_pointer) { |
52 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); | 53 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); |
53 if (current_rva == relocs_start_rva_) { | 54 if (current_rva == relocs_start_rva_) { |
54 if (relocs_start_rva_ < relocs_end_rva_) { | 55 if (relocs_start_rva_ < relocs_end_rva_) { |
55 p += relocs_end_rva_ - relocs_start_rva_; | 56 p += relocs_end_rva_ - relocs_start_rva_; |
56 continue; | 57 continue; |
57 } | 58 } |
58 } | 59 } |
59 | 60 |
60 //while (abs32_pos != abs32_locations.end() && *abs32_pos < current_rva) | 61 //while (abs32_pos != abs32_locations.end() && *abs32_pos < current_rva) |
61 // ++abs32_pos; | 62 // ++abs32_pos; |
62 | 63 |
63 // Heuristic discovery of rel32 locations in instruction stream: are the | 64 // Heuristic discovery of rel32 locations in instruction stream: are the |
64 // next few bytes the start of an instruction containing a rel32 | 65 // next few bytes the start of an instruction containing a rel32 |
65 // addressing mode? | 66 // addressing mode? |
66 const uint8* rel32 = NULL; | 67 const uint8_t* rel32 = NULL; |
67 | 68 |
68 if (p + 5 <= end_pointer) { | 69 if (p + 5 <= end_pointer) { |
69 if (*p == 0xE8 || *p == 0xE9) { // jmp rel32 and call rel32 | 70 if (*p == 0xE8 || *p == 0xE9) { // jmp rel32 and call rel32 |
70 rel32 = p + 1; | 71 rel32 = p + 1; |
71 } | 72 } |
72 } | 73 } |
73 if (p + 6 <= end_pointer) { | 74 if (p + 6 <= end_pointer) { |
74 if (*p == 0x0F && (*(p+1) & 0xF0) == 0x80) { // Jcc long form | 75 if (*p == 0x0F && (*(p+1) & 0xF0) == 0x80) { // Jcc long form |
75 if (p[1] != 0x8A && p[1] != 0x8B) // JPE/JPO unlikely | 76 if (p[1] != 0x8A && p[1] != 0x8B) // JPE/JPO unlikely |
76 rel32 = p + 2; | 77 rel32 = p + 2; |
(...skipping 27 matching lines...) Expand all Loading... |
104 #endif | 105 #endif |
105 p = rel32 + 4; | 106 p = rel32 + 4; |
106 continue; | 107 continue; |
107 } | 108 } |
108 } | 109 } |
109 p += 1; | 110 p += 1; |
110 } | 111 } |
111 } | 112 } |
112 | 113 |
113 } // namespace courgette | 114 } // namespace courgette |
OLD | NEW |