| 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> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "courgette/rel32_finder_win32_x86.h" | 7 #include "courgette/rel32_finder_win32_x86.h" |
| 8 | 8 |
| 9 namespace courgette { | 9 namespace courgette { |
| 10 | 10 |
| 11 Rel32FinderWin32X86::Rel32FinderWin32X86( | 11 Rel32FinderWin32X86::Rel32FinderWin32X86( |
| 12 RVA relocs_start_rva, RVA relocs_end_rva, RVA image_end_rva) | 12 RVA relocs_start_rva, RVA relocs_end_rva) |
| 13 : relocs_start_rva_(relocs_start_rva), | 13 : relocs_start_rva_(relocs_start_rva), |
| 14 relocs_end_rva_(relocs_end_rva), | 14 relocs_end_rva_(relocs_end_rva) { |
| 15 image_end_rva_(image_end_rva) { | |
| 16 } | 15 } |
| 17 | 16 |
| 18 Rel32FinderWin32X86::~Rel32FinderWin32X86() { | 17 Rel32FinderWin32X86::~Rel32FinderWin32X86() { |
| 19 } | 18 } |
| 20 | 19 |
| 21 void Rel32FinderWin32X86::SwapRel32Locations(std::vector<RVA>* dest) { | 20 void Rel32FinderWin32X86::SwapRel32Locations(std::vector<RVA>* dest) { |
| 22 dest->swap(rel32_locations_); | 21 dest->swap(rel32_locations_); |
| 23 } | 22 } |
| 24 | 23 |
| 25 #if COURGETTE_HISTOGRAM_TARGETS | 24 #if COURGETTE_HISTOGRAM_TARGETS |
| 26 void Rel32FinderWin32X86::SwapRel32TargetRVAs(std::map<RVA, int>* dest) { | 25 void Rel32FinderWin32X86::SwapRel32TargetRVAs(std::map<RVA, int>* dest) { |
| 27 dest->swap(rel32_target_rvas_); | 26 dest->swap(rel32_target_rvas_); |
| 28 } | 27 } |
| 29 #endif | 28 #endif |
| 30 | 29 |
| 31 Rel32FinderWin32X86_Basic::Rel32FinderWin32X86_Basic( | 30 Rel32FinderWin32X86_Basic::Rel32FinderWin32X86_Basic( |
| 32 RVA relocs_start_rva, RVA relocs_end_rva, RVA image_end_rva) | 31 RVA relocs_start_rva, RVA relocs_end_rva) |
| 33 : Rel32FinderWin32X86(relocs_start_rva, relocs_end_rva, image_end_rva) { | 32 : Rel32FinderWin32X86(relocs_start_rva, relocs_end_rva) { |
| 34 } | 33 } |
| 35 | 34 |
| 36 Rel32FinderWin32X86_Basic::~Rel32FinderWin32X86_Basic() { | 35 Rel32FinderWin32X86_Basic::~Rel32FinderWin32X86_Basic() { |
| 37 } | 36 } |
| 38 | 37 |
| 39 void Rel32FinderWin32X86_Basic::Find(const uint8_t* start_pointer, | 38 void Rel32FinderWin32X86_Basic::Find(const uint8_t* start_pointer, |
| 40 const uint8_t* end_pointer, | 39 const uint8_t* 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) { |
| 44 // Quick way to convert from Pointer to RVA within a single Section is to | 43 // Quick way to convert from Pointer to RVA within a single Section is to |
| 45 // subtract 'pointer_to_rva'. | 44 // subtract 'pointer_to_rva'. |
| 46 const uint8_t* const adjust_pointer_to_rva = start_pointer - start_rva; | 45 const uint8_t* const adjust_pointer_to_rva = start_pointer - start_rva; |
| 47 | 46 |
| 48 std::vector<RVA>::const_iterator abs32_pos = abs32_locations.begin(); | 47 std::vector<RVA>::const_iterator abs32_pos = abs32_locations.begin(); |
| 49 | 48 |
| 50 // Find the rel32 relocations. | 49 // Find the rel32 relocations. |
| 51 const uint8_t* p = start_pointer; | 50 const uint8_t* p = start_pointer; |
| 52 while (p < end_pointer) { | 51 while (p < end_pointer) { |
| 53 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); | 52 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); |
| 53 |
| 54 // Skip the base reloation table if we encounter it. |
| 55 // Note: We're not bothering to handle the edge case where a Rel32 pointer |
| 56 // collides with |relocs_start_rva_| by being {1, 2, 3}-bytes before it. |
| 54 if (current_rva == relocs_start_rva_) { | 57 if (current_rva == relocs_start_rva_) { |
| 55 if (relocs_start_rva_ < relocs_end_rva_) { | 58 if (relocs_start_rva_ < relocs_end_rva_) { |
| 56 p += relocs_end_rva_ - relocs_start_rva_; | 59 p += relocs_end_rva_ - relocs_start_rva_; |
| 57 continue; | 60 continue; |
| 58 } | 61 } |
| 59 } | 62 } |
| 60 | 63 |
| 61 //while (abs32_pos != abs32_locations.end() && *abs32_pos < current_rva) | |
| 62 // ++abs32_pos; | |
| 63 | |
| 64 // Heuristic discovery of rel32 locations in instruction stream: are the | 64 // Heuristic discovery of rel32 locations in instruction stream: are the |
| 65 // next few bytes the start of an instruction containing a rel32 | 65 // next few bytes the start of an instruction containing a rel32 |
| 66 // addressing mode? | 66 // addressing mode? |
| 67 const uint8_t* rel32 = NULL; | 67 const uint8_t* rel32 = nullptr; |
| 68 | 68 |
| 69 if (p + 5 <= end_pointer) { | 69 if (p + 5 <= end_pointer) { |
| 70 if (*p == 0xE8 || *p == 0xE9) { // jmp rel32 and call rel32 | 70 if (*p == 0xE8 || *p == 0xE9) { // jmp rel32 and call rel32 |
| 71 rel32 = p + 1; | 71 rel32 = p + 1; |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 if (p + 6 <= end_pointer) { | 74 if (p + 6 <= end_pointer) { |
| 75 if (*p == 0x0F && (*(p+1) & 0xF0) == 0x80) { // Jcc long form | 75 if (*p == 0x0F && (*(p+1) & 0xF0) == 0x80) { // Jcc long form |
| 76 if (p[1] != 0x8A && p[1] != 0x8B) // JPE/JPO unlikely | 76 if (p[1] != 0x8A && p[1] != 0x8B) // JPE/JPO unlikely |
| 77 rel32 = p + 2; | 77 rel32 = p + 2; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 88 if (abs32_pos != abs32_locations.end()) { | 88 if (abs32_pos != abs32_locations.end()) { |
| 89 if (*abs32_pos < rel32_rva + 4) { | 89 if (*abs32_pos < rel32_rva + 4) { |
| 90 // Beginning of abs32 reloc is before end of rel32 reloc so they | 90 // Beginning of abs32 reloc is before end of rel32 reloc so they |
| 91 // overlap. Skip four bytes past the abs32 reloc. | 91 // overlap. Skip four bytes past the abs32 reloc. |
| 92 p += (*abs32_pos + 4) - current_rva; | 92 p += (*abs32_pos + 4) - current_rva; |
| 93 continue; | 93 continue; |
| 94 } | 94 } |
| 95 } | 95 } |
| 96 | 96 |
| 97 RVA target_rva = rel32_rva + 4 + Read32LittleEndian(rel32); | 97 RVA target_rva = rel32_rva + 4 + Read32LittleEndian(rel32); |
| 98 // To be valid, rel32 target must be within image, and within this | 98 // Valid, rel32 target must be within image, and within this section. |
| 99 // section. | 99 // Subsumes |target_rva| != |kUnassignedRVA|. |
| 100 if (IsValidRVA(target_rva) && | 100 if (start_rva <= target_rva && target_rva < end_rva) { |
| 101 start_rva <= target_rva && target_rva < end_rva) { | |
| 102 rel32_locations_.push_back(rel32_rva); | 101 rel32_locations_.push_back(rel32_rva); |
| 103 #if COURGETTE_HISTOGRAM_TARGETS | 102 #if COURGETTE_HISTOGRAM_TARGETS |
| 104 ++rel32_target_rvas_[target_rva]; | 103 ++rel32_target_rvas_[target_rva]; |
| 105 #endif | 104 #endif |
| 106 p = rel32 + 4; | 105 p = rel32 + 4; |
| 107 continue; | 106 continue; |
| 108 } | 107 } |
| 109 } | 108 } |
| 110 p += 1; | 109 p += 1; |
| 111 } | 110 } |
| 112 } | 111 } |
| 113 | 112 |
| 114 } // namespace courgette | 113 } // namespace courgette |
| OLD | NEW |