Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / courgette / rel32_finder_x86.cc
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
3 // found in the LICENSE file.
4
5 #include "courgette/rel32_finder_x86.h"
6
7 namespace courgette {
8
9 Rel32FinderX86::Rel32FinderX86(RVA relocs_start_rva, RVA relocs_end_rva)
10     : Rel32Finder(relocs_start_rva, relocs_end_rva) {}
11
12 // Scan for opcodes matching the following instructions :
13 //  rel32 JMP/CALL
14 //  Jcc (excluding JPO/JPE)
15 // Falsely detected rel32 that collide with known abs32 or that point outside
16 // valid regions are discarded.
17 void Rel32FinderX86::Find(const uint8_t* start_pointer,
18                           const uint8_t* end_pointer,
19                           RVA start_rva,
20                           RVA end_rva,
21                           const std::vector<RVA>& abs32_locations) {
22   // Quick way to convert from Pointer to RVA within a single Section is to
23   // subtract |adjust_pointer_to_rva|.
24   const uint8_t* const adjust_pointer_to_rva = start_pointer - start_rva;
25
26   std::vector<RVA>::const_iterator abs32_pos = abs32_locations.begin();
27
28   // Find the rel32 relocations.
29   const uint8_t* p = start_pointer;
30   while (p < end_pointer) {
31     RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva);
32
33     // Skip the base reloation table if we encounter it.
34     // Note: We're not bothering to handle the edge case where a Rel32 pointer
35     // collides with |relocs_start_rva_| by being {1, 2, 3}-bytes before it.
36     if (current_rva >= relocs_start_rva_ && current_rva < relocs_end_rva_) {
37       p += relocs_end_rva_ - current_rva;
38       continue;
39     }
40
41     // Heuristic discovery of rel32 locations in instruction stream: are the
42     // next few bytes the start of an instruction containing a rel32
43     // addressing mode?
44     const uint8_t* rel32 = nullptr;
45
46     if (p + 5 <= end_pointer) {
47       if (p[0] == 0xE8 || p[0] == 0xE9) {  // jmp rel32 and call rel32
48         rel32 = p + 1;
49       }
50     }
51     if (p + 6 <= end_pointer) {
52       if (p[0] == 0x0F && (p[1] & 0xF0) == 0x80) {  // Jcc long form
53         if (p[1] != 0x8A && p[1] != 0x8B)           // JPE/JPO unlikely
54           rel32 = p + 2;
55       }
56     }
57     if (rel32) {
58       RVA rel32_rva = static_cast<RVA>(rel32 - adjust_pointer_to_rva);
59
60       // Is there an abs32 reloc overlapping the candidate?
61       while (abs32_pos != abs32_locations.end() && *abs32_pos < rel32_rva - 3)
62         ++abs32_pos;
63       // Now: (*abs32_pos > rel32_rva - 4) i.e. the lowest addressed 4-byte
64       // region that could overlap rel32_rva.
65       if (abs32_pos != abs32_locations.end()) {
66         if (*abs32_pos < rel32_rva + 4) {
67           // Beginning of abs32 reloc is before end of rel32 reloc so they
68           // overlap. Skip four bytes past the abs32 reloc.
69           p += (*abs32_pos + 4) - current_rva;
70           continue;
71         }
72       }
73
74       // + 4 since offset is relative to start of next instruction.
75       RVA target_rva = rel32_rva + 4 + Read32LittleEndian(rel32);
76       // Valid, rel32 target must be within image, and within this section.
77       // Subsumes |target_rva| != |kUnassignedRVA|.
78       if (start_rva <= target_rva && target_rva < end_rva) {
79         rel32_locations_.push_back(rel32_rva);
80 #if COURGETTE_HISTOGRAM_TARGETS
81         ++rel32_target_rvas_[target_rva];
82 #endif
83         p = rel32 + 4;
84         continue;
85       }
86     }
87     p += 1;
88   }
89 }
90
91 }  // namespace courgette